0

i am sure this question is asked before but i was not able to find the right answer.

I have this user model schema and i want to save it into my mongo db. The saving process it works just fine but i can't return the user data afterwards.

I think the whole problem where i get confused is the function with in the save method which handles this callback.

Let me show you my code:

var saveUser = user.save(function(err, arg){
    if (err) {
        console.log(err);
        return err; // in case of error
    }
    return arg; // the user object i want to assign to the saveUser variable
}); 

 //the setTimeout to make sure the callback is finished
 setTimeout(function(){
    console.log(saveUser); // the return (should have data at this moment)
}, 1000);

Thanks in advance.

itismelito
  • 245
  • 3
  • 11
  • **You must not use** `setTimeout` to make sure the callback has finished. Instead, just put the `coneole.log(arg)` in that callback. – Bergi Jul 08 '15 at 00:24
  • And of course, `user.save` doesn't return the result of that asynchronous callback, so `saveUser` always is `undefined` even if the callback had been executed. – Bergi Jul 08 '15 at 00:26

0 Answers0