If you want to know what's the problem in this thread in two words: I'm trying to accomplish a mongoose function findOrAdd(): if the Id I'm searching for isn't present, I should add a new document. After that (that's why I need some sync functions) I need to do another query based on the new ObjectIds.
This is my Post Schema
var com_post_schema = new Schema({
content: { type: String, required: true },
postedBy: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'com_user'
}]
});
and my User Schema
var com_user_schema = new Schema({
name: { type: String, required: true },
age: {type:Number}
});
So a post can have more than one author. My problem: an author can be an existent user (chosen in bootstrap-tokenfield) or a new user, see this json example:
{
"content":"post content",
"postedBy":[
{
"_id":"56a60a972b70225014753d1a",
"name":"Paul",
"age":20,
"__v":0,
"value":"Paul",
"label":"Paul"
},
{
"value":"John",
"label":"John"
}
]
}
The user Paul is already present in 'com_user' collection, I have to save the user John in 'com_user' and then save the post with both user ObjectIds refs (the fields 'value' and 'label' are sent by bootstrap-tokenfield). I'm not clear how can I do that.
EDIT this is my current code I still have sync problems. I made some tests and I see randomly in console "New post added" and then "User not found.."
Try with 3 users and see..
app.post('/api/community/posts', function(req,res){
var arr=[],i=0;
req.body.postedBy.forEach(function(el){
com_user.findById(el._id, function (err, user) {
if(user) {
console.log("User found!");
console.log(user);
arr.push(mongoose.Types.ObjectId(user._id ));
i++;
if(i==req.body.postedBy.length-1) {
console.log('UFi'+i)
console.log(arr)
var com_post1= new com_post({
content:req.body.content,
postedBy:arr,
});
com_post1.save(function(err){
if(!err){
console.log("New post added!");
res.json({"New post added! ":req.body.content});
}
else {
res.json({"Error adding post":'error'});
error(err)
}
});
}
}
else {
var com_user1= new com_user({
name:el.label,
age: 20
});
com_user1.save(function(err,newuser){
if(err)
console.log(err)
else {
console.log('User not found and just added!');
console.log(newuser)
arr.push(mongoose.Types.ObjectId(newuser._id));
console.log(arr)
i++;
if(i==req.body.postedBy.length-1) {
console.log('NUFi'+i)
console.log(arr)
var com_post1= new com_post({
content:req.body.content,
postedBy:arr,
});
com_post1.save(function(err){
if(!err){
console.log("New post added!");
res.json({"New post added! ":req.body.content});
}
else {
res.json({"Error adding post":'error'});
error(err)
}
});
}
}
});
}
});
});
});
its because the code after forEach gets executed before the forEach is completed. Try it like this
});