I want to populate a non-id field with mongoose, and already found these questions here:
It turns out that mongoose didn't support this functionality now. The open issue here:https://github.com/Automattic/mongoose/issues/2562
So now, I want to populate manually with extra queries. But I have some problems in doing it
I have two schema (replies, users):
Here is the replies schema, which the author_id referencing to the user's collection's username field not _id;
var ReplySchema = new Schema({
'content' : {
type: String,
required: true
},
'author_id' : {
type: String,
ref: 'users'
},
});
When I got the entire replies. like:
replies [ {
content: 'comments one',
author_id: 'raynlon',
},
{
content: 'comments two',
author_id: 'raynlon',
} ]
FROM here, i'm doing a further step to query the author from User table, what I did is:
var replies = yield query.exec(); // which already got all the replies
var final = [];
for (var i = 0; i < replies.length; i++) {
co(function*(){
var data = yield User.Model.findOne({username: replies[i].author_id}).select('username email avatar gravatar_id name').exec();
return data;
}).then(function(value) {
final.push(value);
});
}
return final // I need to return the modified data back to the client
I always got empty [];
this is due to the async code inside for loop?
I also found some solutions, like: Promise.all(), q.all() these kinda parallel promise stuff....
but I don't know how to do..
the co library I used said the co() should returned as a promise, theoretically, we could then inject into Promise.all([]), but it doesn't work...so could anyone help me about this?