how to get data from fetch to a variable? - json

so i'm fetching this api using the fetch
fetch("https://public-api.solscan.io/transaction/"+JSON.parse(message.data).params.result.value.signature)
.then(response => response.json())
.then(response => console.log(JSON.stringify(response.parsedInstruction[1].params.mint)))
.catch(error => nothing());
but i need
JSON.stringify(response.parsedInstruction[1].params.mint
in a variable so i can make another api request please help me
this is the code where i want to use with the variable
const mintAddress = new PublicKey("7brV5ykCzriKhVhj99kav97LQQrpb18J3kMonGxveML2");
const nft = await metaplex.nfts().findByMint({ mintAddress }).run();
const png = nft.json.image ;
console.log(nft.json)

Related

Validate and store data in Laravel with axios

I am using React\NextJS as forntend and Laravel as backend,
Storing data with Axios post as per below,
const storeExpense = async (expenseData) => {
const response = await axios.post('/api/expenses/store/', {expenseData})
return response.data;
}
Now this will be sent as a JSON object to Laravel, I am not sure how can I validate and store this JSON object data to MySQL.
Earlier I was using Jquery AJAX where it was easy to store with Request validation and then with create.
Below is the request payload to the backend,
{"expenseData":{"expense_description":"React","expense_date":"2022-17-4","expense_amount":"123","expense_tax_amount":"14.15","expense_note":"given","expense_receipt_number":"Ok no","taxgroup_id":1,"paymentoption_id":1,"vendor_id":1,"accountcustomtype_id":2,"submit":null}}
Below was Controller and Model, used when sending data as Form
//Controller
public function store(StoreExpenseRequest $request)
{
Expense::storeExpense($request);
return response()->json([
'val' => 1,
'msg' => 'Success',
]);
}
//Model
public static function storeExpense($request)
{
Expense::create([
'vendor_id' => $request->vendor_id,
'accountcustomtype_id' => $request->accountcustomtype_id,
'expense_description' => $request->expense_description,
'expense_date' => Carbon::parse($request->expense_date)->format('Y-m-d'),
'expense_amount' => $request->expense_amount,
'taxgroup_id' => $request->taxgroup_id,
'expense_tax_amount' => $request->expense_tax_amount,
'paymentoption_id' => $request->paymentoption_id,
'expense_receipt_number' => $request->expense_receipt_number,
'expense_note' => $request->expense_note,
]);
}
But now this doesn't work with JSON data,
How can I achieve this with Axios JSON data?
Thank you,
It was resolved by changing Axios request Data,
await axios.post('/api/expenses/store/', expenseData)

How to Fetch specific api from database?

componentDidMount() {
fetch("http://localhost:5000/customers/:id")
.then((res) => res.json())
.then((info) => {
this.setState({ info });
})
.then((err) => console.log(err));
}
How to fetch a specific data from database by sending the required ID with URL?
You can use the format string syntax from javascript and add the id to the url-string with that.
const id = 42
fetch(`http://localhost:5000/customers/${id}`)

How do you Fetch a nested JSON key value then assign to variable?

I'm not able to pick out a certain key/value from a fetched JSON file, and then assign it to a variable.
const fetch = require("node-fetch");
var url = 'https://randomuser.me/api/'
fetch(url)
.then((response) => response.json())
.then((hmm) => console.log(hmm.results))
The above code serves to return info from the top branch, "results", but I can't get any deeper.
Modifying it to .then((hmm) => console.log(hmm.results.gender)) returns undefined.
I'd then like to assign the Value from the "gender" Key to a variable.
results is an array.
You need to do results[0].gender
.then((hmm) => console.log(hmm.results[0].gender))
const fetch = require("node-fetch");
var url = 'https://randomuser.me/api/'
fetch(url)
.then((response) => response.json())
.then(function (hmm) {
var data = hmm.results[0].gender;
console.log(data)
})

Pulling Data from JSON with React-Native

What's wrong with this picture ? :-)
I'm trying to pull values from my wordpress JSON objects.
I must not be addressing them the correct/proper way with React-Native.
Can someone direct me as to how to form the correct console log statement ?
As it stands right now, the variable 'theMediaLink' is coming up 'undefined' in the console log. :-(
state={
data:[]
}
fetchData = async() => {
const response = await
//response
fetch('http://example.com/wp-json/wp/v2/posts/')
//posts
const posts = await response.json();
this.setState({data:posts});
const theMediaLink = `https://example.com/wp-json/wp/v2/media/` + `${this.state.data.featuredmedia}`;
console.log('theMediaLink_value: ', theMediaLink);
}
I should add that the fetch is retrieving data as I use it later on with a FLATLIST to pull the posts. My question is I must be misforming the call, but how ?
This is more an issue of understanding of React rather than React Native.
Try this:
fetchData = async () => {
const URL = 'http://example.com/wp-json/wp/v2/posts/'
const response = await fetch(URL)
const posts = await response.json();
return this.setState({ data : posts }, () => {
const { data } = this.state
const theMediaLink = `https://example.com/wp-json/wp/v2/media/${ data.featuredmedia }`;
console.log('theMediaLink_value: ', theMediaLink);
});
}
Since setState is asynchronous, you won't be able to see the state update right away, that's why the callback is for.
More info on how to setState asynchronously and use its value after right here

Determine if Axios request is done

Is there any way to determine if a Axios request like the following received an answer and is done?
axios.get('/api')
.then(response => this.data = response.data);
Yes, this is a promise and you can reference and then it:
var req = axios.get('/api')
.then(response => this.data = response.data);
Now I want to react to it being done:
req.then(x => console.log("Done!"));
Or save that state outside:
var isReqDone = false;
req.then(x => isReqDone = true);