I'm building a twitter clone using Node.js and MongoDB with mongoose. My Tweet model has body, user and created fields where user is the id of the user who has created the tweet. Now I'm building the API. I want when I make a GET request to receive a list of all the tweets (/api/tweets/) but except the user field (which returns only the id of the user) I want to get the whole user object so that I can display information about the tweet owner in my front-end part. I ended up with the following code.
exports.all = function (req, res, next) {
Tweet.find({}, function (err, tweets) {
if (err) return res.json(400, err);
var response = [];
tweets.forEach(function (element, index, array) {
var tweet = {};
tweet._id = element._id;
tweet.created = element.created;
tweet.body = element.body;
User.findById(element.user, function (err, user) { // <- This line
if (err) return res.json(400, err);
tweet.user = user;
});
response.push(tweet);
});
return res.json(response);
});
};
It works perfectly except that it doesn't add the user info. The problem is in the line I have marked. When javascript comes to that line, it tries to make it "parallel" and continues with the code without executing the callback function. But then it pushes the tweet object that doesn't have yet user info. How can I fix this?
You're going to want to use the async library. It will make your life much easier.
// inside `Tweet.find`
async.each(tweets, function(done) {
// do stuff to tweets
User.findById(element.user, function(err, user){
if (err) return done(err);
// do stuff with user
done();
});
}, function(err) {
// called when done
res.json(response);
});
The issue is that res.json sends the response so it doesn't matter that findById is being called. You need to call res.json once everything is done.
You can do this in several ways, but the easiest one with your existing code is to just keep a counter:
var tweetCount = 0;
tweets.forEach(/* snip */) {
User.findById(element.user, function (err, user) {
tweet.user = user;
tweetCount++;
response.push(tweet);
if (tweetCount == tweets.length) {
res.json(response);
}
});
});
You can use Q promise library to sync. It is simple and easy to use.
Response will only be send when whole promise chain is compeleted
var result = Q();
tweets.forEach(function (tweet, i) {
result = result.then(function () {
// do your stuff
}
result.then(function () {
res.json(response); //successfully completed
});
Related
I am accessing an API endpoint that is a little iffy. About 70% of the time it sends back the correct JSON response, but a few times it will crap out and send an XML that says "values /values". I want to make a while loop what requests until it gets the correct response back. In my case, I am guaranteed to get back the correct response eventually, so that is why I am looping instead of figuring out why the endpoint is crapping out.
Here is my code:
var gotValidResponse = false;
while(!gotValidResponse){
request(options, function(err, res, body){
if(err){
console.log(err);
return;
}
try{
data = JSON.parse(body);
console.log(data);
gotValidResponse = true;
}catch(e){
console.log('trying again');
}
});
}
processJSON(data);
Obviously the code above does not work, but hopefully it is showing what I am trying to do. Thanks for any help!
EDIT: Like this?
var myData = getStuff(options);
function getStuff(options){
request(options, function (err, res, body) {
if (err) {
console.log(err);
return
}
try {
data = JSON.parse(body);
return data;
} catch (e) {
return getStuff(options);
}
})
}
You almost got it right in your edit. What you need to do is keep calling the function until it returns what you want. Like this (my conditionals are merely illustrative):
var attemps = 1;
var fake = function(data, cb) {
console.log('Attemp nÂș', attemps);
if(attemps < 5) {
attemps += 1;
return fake(data, cb);
} else {
return cb(null, 'completed');
}
}
fake('whatever', function(err, res) {
console.log(res)
})
https://jsfiddle.net/eysu2amp/
If you check the console, you will see that the fake function gets called 5 times and then returns the data. The recursive calling of the function keeps passing the same callback function.
I'm new to NodeJS and Jade/PUG so I know this can be a really simple question for many of you but for me I can't understeand any of the answers a get from the internet because the term 'Promise' and how it works is a little "confusing" for me.
I am querying a postgre database to get several values from a table (really simple query). If I do this without using a promise everything works fine, console prints the result and everyone is happy, but when I try to store this result into a variable and pass it as a parameter to a Jade template things change.
I have read that, in order to do that, I need to use promises because it is more likely that when the variable is being accessed, the value might not be resolved yet and that's what Promises are for. So here I have my code:
hero.js:
getHeroes: function()
{
//Initialize array
var elem = [];
console.log('Querying heroes');
return new Promise((resolve, reject) =>
{
pg.connect(conString, function (err, client, done)
{
if (err)
{
return console.error('error fetching client from pool', err)
}
//Execute SELECT query
client.query('SELECT name from heroe;', function (err, rows, result)
{
//Iterate over results
for (var i = 0; i < rows.rowCount; i++)
{
//PUSH result into arrays
elem.push(rows.rows[i].name);
}
done()
if (err)
{
return console.error('error happened during query', err)
}
resolve(elem)
})
});
})
}
And this the part of my server.js where I call that function:
app.get('/jade', (request, response) => {
var heroList = [];
heroList = hero.getHeroes();
console.log(heroList);
response.render('test_jade', {param1: 'test'});
})
that console.log shows up "Promise { pending }" and I don't know how to "listen to the resolved event and retrieve the value from it once it has finished".
Would appreciate any advice/solution or even a good Node.js manual where all this mess is explained for total newbies like me.
Thanks in advance!
It's not how you use promise.
Try this,
app.get('/jade', (request, response) => {
var heroList = [];
hero.getHeroes().then(data=>{
heroList = data;
console.log(heroList);
response.render('test_jade', {param1: 'test'});
}).catch(e=>{
//handle error case here when your promise fails
console.log(e)
})
})
You should also catch in case your promise fails.
Code
Server.js result
My code is this
app.post('/get_page_data', function(req, res) {
function find (i,documents)
{
Item_data.find({item_name:documents[i].item_name}).exec(function (err,asd){
console.log(documents[i].item_name+": found");
console.log(asd[0].like);
}
)
}
Page_data.find().lean().exec(function (err, documents) {
var doc=documents;
async.series([
// 1st
function(done){
for(var i=0;i<documents.length;i++)
{
find(i,doc);
done()
}
},
// 2nd
function(done){
console.log("1");
console.log(doc);
console.log("2");
res.end(JSON.stringify(doc));
console.log("3");
done()
}
]);
}
)});
I'll introduce my code simply
when ajax called /get_page_data
i bring the pages data (page1,page2,page3 ...) in documents
every page has a item_name but pages data does not have the "like" data
so i find the value of "like" in the other collection(Item_data) by the same item name and put the "like" in the pages data documents
but this makes me crazy
the pages data is sent before i put the like value
so i read about the non blocking io and async blahblah...
and i found the async module. And used it.
But as you can see the pictures above, they're not working
(before trying async moudle they didn't work neither)
I don't know why res.end part excutes previously and Please tell me the solution of this situation Thank you for reading
Your find function is doing an asynchronous call, so the done() is being called before it returns any data.
You should move it inside the inner find callback.
Edit: Using async.each instead of async.series directly.
app.post('/get_page_data', function(req, res) {
function find (document, cb) {
Item_data.find({item_name:document.item_name}).exec(function (err,asd) {
console.log(document.item_name+": found");
console.log(asd[0].like);
cb();
})
}
Page_data.find().lean().exec(function (err, documents) {
var doc=documents;
async.each(documents,
// 1st
find(document, cb),
// 2nd
function() {
console.log("1");
console.log(doc);
console.log("2");
res.end(JSON.stringify(doc));
console.log("3");
});
}
)});
PS: there's also a lot of things that you should improve in that code. But that way it should work.
put the done thing inside the aysn function and call the done when the index value
app.post('/get_page_data', function(req, res) {
function find (i,documents,cb)
{
Item_data.find({item_name:documents[i].item_name}).exec(function (err,asd){
console.log(documents[i].item_name+": found");
console.log(asd[0].like);
console.log(i +"/"+ documents.length);
documents[i]["like"]=asd[0].like;
if(i==documents.length-1) cb();
//call done() when the index reached maximum
})
}
Page_data.find().lean().exec(function (err, documents) {
var doc=documents;
async.series([
// 1st
function(done){
for(var i=0;i<documents.length;i++)
{
find(i,doc,done); //put the done thing inside the function
}
},
// 2nd
function(done){
console.log("1");
console.log(doc);
console.log("2");
res.end(JSON.stringify(doc));
console.log("3");
done()
}
]);
working properly
app.get('/ratings', function (req, res){
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount=doc;
});
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount=doc;
});
I am new to using the MEAN stack and trying to return the values of the number of 5 and 4 star reviews. I can retrieve the number of 5 stars just fine, but when I do multiple , I seem to be running into trouble and not sure how to set it up.
You kind of need to get the gist of how to handle control flow while doing async operations.
The function execution would not stop, unlike php, when you are running an async operation.
To accomplish what you are trying to do, the second db call needs to be inside the callback of the first. Try this out.
app.get('/ratings', function (req, res, next) {
db.database.find({rating:5}).count(function(err, doc) {
// always handle error first
if (err) {
return next(err);
}
if (doc) {
review.FiveStarCount = doc;
}
db.database.find({rating:4}).count(function(err, doc) {
if (err) {
return next(err);
}
if (doc) {
review.FourStarCount = doc;
}
console.log(review.FiveStarCount, review.FourStarCount);
});
});
});
So it seems like you are running against the async nature of JavaScript. in the case of the functions above, they will not execute in the order you have written (and you wouldn't want them too!) because they are async. There are few simple solutions to get you going while you learn more about the problem.
Solution 1: Nested callbacks. Hard to read. also, it waits for the first to complete before firing the second, possibly slowing everything down.
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount = doc;
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount = doc;
// DO something with data
res.json(review);
});
});
Solution 2: Latch. This has the advantage of making the requests in parallel and not being as awful to read.
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
});
There are course more solutions, such as maybe a better query or promises. Maybe read a bit more about async JavaScript here to get a better understanding.
I'm not clear as to why return is used or not in the following code. Please explain when it should and shouldn't be used or required to return the response.
update: function(req, res) {
var id = req.param('id');
User
.update(id, req.params.all())
.exec(function(err, users) {
if(err) return res.json(err, 400);
var user = users[0];
console.log('ID', user.id);
User
.findOne(user.id)
.populate('profile')
.exec(function (err, user){
if (err) return res.json(err, 400);
res.json(user, 201);
});
});
},
return is used in the code in question as opposed to else. The code could be rewritten as:
update: function(req, res) {
var id = req.param('id');
User
.update(id, req.params.all())
.exec(function(err, users) {
if(err) {res.json(err, 400);}
// note the lack of "return" above, because in the case of an error
// the code below will never run
else {
var user = users[0];
console.log('ID', user.id);
User
.findOne(user.id)
.populate('profile')
.exec(function (err, user){
if (err) {res.json(err, 400);} // again, no return
else {res.json(user, 201);}
});
}
});
};
But that requires you to keep track of more braces, and is possibly less efficient (although v8 probably optimizes the else in these cases). You'll also see this convention used often when callbacks are involved:
function (req, res, next) {
if (!req.session.loggedInUser) {
return res.forbidden();
}
return next();
}
Again, you could rewrite using with else:
function (req, res, next) {
if (!req.session.loggedInUser) {
res.forbidden();
} else {
next();
}
}
Notice an important difference here: in the elseless code, if you leave out the first return you'll get into trouble because you'll send the res.forbidden response and execute the next() method, which you almost certainly don't want. On the other hand in the returnless code, if you were to add some more code after the if/else, it would get executed no matter what, which you also don't want. So a good convention is to always return when you're using a callback (like next()) or executing code that will send a full response to the client (like res.send or res.json).
return isn't used because there's a possibility that the code will be doing asynchronous operations. Async operations run "in parallel" or "later" or "just not in sequence", thus breaking the sequence of your code (Think of threads).
The usual practice is to provide a function (usually called a "callback") or an object that hosts a bunch of functions that can be called when your async operation finishes, instead of a return.