To be clear about Emanuele's response, the async API should look like this.

async.each(contents, function(content, cb) {
     content.save(function(err, resp) {
         cb(err); //lets hope err is null, but if it's not null, the err 
will get propagated through to the final "errors" param below.
     });
}, function(errors) {
    res.status.(200).. ///OK
});


-Chris

On Monday, February 22, 2016 at 10:01:38 AM UTC-7, Emanuele DelBono wrote:
>
> Probably you are using async.js in a wrong way. Do you call the "done 
> callback" on after every save of the chest? The callback for async.each 
> received 2 parameters, the item and the done callback.
>
>
> On Mon, Feb 22, 2016 at 3:55 AM, Duy Nguyen <[email protected] 
> <javascript:>> wrote:
>
>> Hi,
>>
>> I want to my code work as below:
>> 1. save Chest object to db
>> 2. Then save all contents belong to this chest to db (have logic to add 
>> new filed to content before saving)
>> 3. Then return response with chest + new contents info
>>
>> But the real code does not work as I expected, the code is:
>>
>> chestApi = createChestWithContents{
>>>     var chest = req.body;
>>>     var contents = req.body.contents;
>>>     chest.save(function(err, chest) {
>>>            async.each(contents, function(content) {
>>>                      content.save()....
>>>             }, function(done) {
>>>                    return res.status.(200).. ///OK
>>>             });
>>>     });
>>> }
>>
>>
>> When I use postman to test, the method end up without any response 
>> returned due to asynchronous methods(even when I used async to wait till 
>> the loop finish), cannot figure out how to do those stuff synchronously.
>> -- 
>> Nguyen Hai Duy
>> Mobile : 0914 72 1900
>> Skype: nguyenhd2107
>>
>> -- 
>> 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] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/nodejs/CAGOR8-2A8LGnQugzH92quVwEDCiGOBExp7_mq%3DAtjp-LaDt4_Q%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/nodejs/CAGOR8-2A8LGnQugzH92quVwEDCiGOBExp7_mq%3DAtjp-LaDt4_Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> ema
> http://ema.codiceplastico.com <http://blog.codiceplastico.com/ema>
>

-- 
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/0bb3416a-c622-40ea-a5ab-bfcd93a173fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to