Reputation: 1249
I'm using Nodejs and Unirest and have the following code :
import u from 'unirest';
u.get(firstUrl).query(q).end(function (response) {
if (response.status == 200) {
posts = response.body;
console.log("Step 1");
} else {
console.log("error");
}
console.log("Step 2");
u.get(secondUrl).query(q).end(function (response) {
if (response.status == 200) {
events = response.body;
console.log("Step 3");
} else {
console.log("error");
}
});
console.log("Step 4");
response.render('myPage', {
posts: posts, `
events:events
});
});
The first request works very well (posts are sent to the view) but i'm not able to get events from the view. In my terminal, i have :
Step 1
Step 2
Step 4
Step 3
Can you please help me to get both posts and events before rendering "myPage". Thanks for your valuable help.
Upvotes: 0
Views: 411
Reputation: 2476
I suggest you to look into promises, you will get a better understanding of the flow.
But like this will work ;)
import u from 'unirest';
u.get(firstUrl).query(q).end(function (response) {
if (response.status == 200) {
posts = response.body;
console.log("Step 1");
} else {
console.log("error");
}
console.log("Step 2");
u.get(secondUrl).query(q).end(function (response) {
if (response.status == 200) {
events = response.body;
console.log("Step 3");
} else {
console.log("error");
}
console.log("Step 4");
response.render('myPage', {
posts: posts, `
events:events
});
});
});
Upvotes: 1