4

Im trying to save data from GET request into variable using node-fetch, but i got the some result! When i console log the response, i can see it. But when i assign the resData into variable, i get undefined.

const fetch = require('node-fetch');

async function fetchData(){
const response = await fetch(url, options)
const resData = await response.json();
console.log(resData);
return resData; 
 };
 
let myApps
 
fetchData((data)=>{
 myApps = data;
});
 
console.log(myApps);

result ==> undefined

Someone can help ME!

2
  • 2
    const myApps = await fetchData(); remember to execute it in an async method as well to use await. Commented Jun 18, 2021 at 10:39
  • fetchData is executing asynchronously , and your assignment myApps = data; statement execute before result returned. you need to use await here as well Commented Jun 18, 2021 at 10:40

1 Answer 1

2

Your console.log executes before your network request is completed. This is because HTTP requests are asynchronous. The fetchData method returns a Promise. The correct implementation should be like this:

const fetch = require('node-fetch');

async function fetchData(){
     const response = await fetch(url, options)
     const resData = response.json();
     console.log(resData);
     return resData; 
};
 
let myApps

fetchData().then((data) => {
   // the network request is completed
   myApps = data;
   console.log(myApps);
}).catch((e) => {
   // Network request has failed
   console.log(e);
});

// or using await if your parent method is an `async` function
try {
   myApps = await fetchData()
} catch (e) {
   // Network request has failed
   console.log(e);
}

Update: After the comment of OP

To send a response of fetchData in an API call using express

async function getData(req, res) {
  try {
     const data = await fetchData();
     // you can loop on your data as well
     
     // send the response
     res.json({ data });
  } catch (e) {
     res.status(503).json({ msg: "Internal Server Error" });
  }
}

// somewhere in your route
app.get("/data", getData);

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the response. The problem is i want save the response into variable and make a for loop to get specific data!
How do you want to call the fetchData? as soon as the application starts? or on any event?
Using express, i want to get data from fetchData and next i make route of GET using express that return the result of fetchData. For example, data from fetchData is list of apps, i want to make GET request using express return .json()
then call fetchData method in your controller. like async function(req, res) { data = await fetchData() res.json({ data ); }
@Redink yes you can achieve the same. In your server, you can run some asynchronous code and assign the value to the global variables and then call app.listen to start the server so when the server starts you can assure that all the async calls are resolved.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.