Hi guys, I'm struggling with syncronous operations in node.js
I have "posts" and "user" collections. A post can have more than one author.
My post schema

var com_post_schema = new Schema({
content: { type: String, required: true },
postedBy: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'com_user'
    }]
});


My user schema

var com_user_schema = new Schema({
    name: { type: String, required: true },
    age: {type:Number}
});


My sample json

{
   "content":"post content",
   "postedBy":[
      {
         "_id":"56a60a972b70225014753d1a",
         "name":"Paul",
         "age":20,
         "__v":0,
         "value":"Paul",
         "label":"Paul"
      },
      {
         "value":"John",
         "label":"John"
      }
   ]
}


As you can see in json, postedBy can be an existing user (with its 
ObjectId) or a new user. If so, I have to save the user and then save the 
post with the user refs objectId both.

This is my code (node.js, express, mongoose), please help me!

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)
                                }
                            });
                        }
                    }
                });
            }
        });
    });



-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/26b2dcaa-101e-4f98-99d7-95147b91eb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to