0

When a user register, he's profile must get some values from another website.

I have written a function that returns these values, but it won't work, as I want it to.

Here's the code:

var user = new User(); // to create him in the database
user.userData.someVariable = myFunc(someID);

function myFunc(someID){
.......
  Users.findOne({'someVar': someID}, function(err, user){
    if(err)
      throw err;
    if(user)
      return user.userData.id; // it doesn't return it, but it has found it
      // I can even console.log it, and I see that it exists, but user.userData.someVariable wont be set to user.userData.id
    else if(!user) throw err;
  }   
}

Any help? Is there something else I can do to get the value? Thank you!

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Jim The Greek
  • 31
  • 2
  • 7
  • I want it to be returned to the second line(on the code that I wrote here), so that user.userData.someVariable is set to the id returned by the function (myFunc()). When I see my database to see if it's actually there, user.userData.someVariable does not exists at all. The function is also called at the second line (on the code that I wrote here, again) – Jim The Greek Dec 28 '15 at 12:56
  • My bad I completely overlooked that part. Sorry. – Gandalf the White Dec 28 '15 at 12:57
  • if(user && user.userData && user.userData.id) use instead may help but if not then show the code where return value is going – Rahul Kamboj Dec 28 '15 at 13:01

1 Answers1

1

You need to pass a callback instead of just returning the value, by the time you ask for the value, the request might have not been completed. Example:

var user = new User(); // to create him in the database

// Call myFunc and wait for it to finish.
myFunc(someID, function(err, userData){
  if (err) throw err;
  user.userData.someVariable = userData;
});

function myFunc(someID, callback){

  Users.findOne({'someVar': someID}, function(err, user){
    if(err)
      return callback(err);
    if(user)
      return callback(null, user.userData.id);
    else if(!user) return callback(new Error('User not found!'));
  }   

}
Juan Stiza
  • 523
  • 2
  • 15
  • We don't need *yet another answer* to this question. – T.J. Crowder Dec 28 '15 at 12:58
  • Agreed, I should have commented! – Juan Stiza Dec 28 '15 at 13:17
  • Ok, that seems to work without errors. But were do the other variables go? (I mean in the code) I still need to set user.userData.userName and user.userData.password. Where do I place them (again, in the code!)? Thank you for helping me! – Jim The Greek Dec 28 '15 at 13:23
  • Well, on the callback, instead of passing `user.userData.id` you could just pass the whole `user` variable ;) – Juan Stiza Dec 28 '15 at 13:25
  • @JuanStiza I can't quite do that because the other variables are set on the html, so there is nothing to search in the db. They must be set before or after the myFunc(someID... etc. Not inside it! Anything else? – Jim The Greek Dec 28 '15 at 13:40
  • Well, in that case, you should set the those parameters in a single object, and it should be on the same scope, that will depend on how the rest of the code looks. – Juan Stiza Dec 28 '15 at 13:46
  • @JuanStiza Let me write you the code because it's kinda hard otherwise! `app.post('/register', function(req, res){ var user = new User(); user.userData.username = // the callback function I need user.userData.password = req.body.password; user,userData.email = req.body.email; user.save(function(err){ if(err) throw err; }) })` As you see, I can't find the rest of the data in the db, I'm getting the straight from the client. Thank you for helping me! – Jim The Greek Dec 28 '15 at 13:59
  • Never mind. I used the process.nextTick() function and it worked properly. Thank you! – Jim The Greek Dec 28 '15 at 14:49