3

I have been working on an authentication system lately that is supposed to either create a new user if no user with a given ID exists OR loads in a user with an existing ID. I also already figured out that you can return whether or not a user exists with in ID by using

User.count({_id: id}, function (err, count){ 
    if(count > 0){
      // User exists, continue with loading data
    } else {
      // User does not exist, create one
    }
  });

with User being an exported Mongoose Model mongoose.model('User', UserSchema);

If I created a function existsWithId(id) in my app.js now, it would work perfectly fine (judging by the fact that the previously mentioned code works perfectly fine), but I want to move this function to my user.js Model (which contains the Mongoose / MongoDB Code) instead so that I have it only for that specific model. Whenever I try to do that by using

UserSchema.statics.existsWithId = function(id) {
  this.count({_id: id}, function (err, count){ 
    return count > 0;
  }); 
}

or similar methods, though, it will either just return undefined or an error once I run it inside of my app.js.

NOTE: I'm sorry if this is a kind of simple or even foolish question, but I am pretty new considering DB management with Mongoose and while I do have SOME experience with JS, I am not exactly a professional at it.

If any of you need additional code provided to answer the question, feel free to ask me and I will do so. Also, I can't +rep answers yet, but I always appreciate 'em :)

Max H.
  • 45
  • 1
  • 7

1 Answers1

1

Your existsWithId method returns a value asynchronously so you can give a callback to it to get the result back like this :

UserSchema.statics.existsWithId = function(id, cb) {
    this.count({ _id: id }, function(err, count) {
        cb(err, count > 0);
    });
}

User.existsWithId("5882c3be5aad09028d4d8b46", function(err, res) {
    console.log(res);
});
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • Works nicely, thanks! One question though. I do not really understand the whole "findByID" concept of Mongoose yet: How would I set the `user` Object to the existing one now (I tried `user = User.findOne({ _id: id });`, but that apparently returns something else.) [EDIT: I just remembered, it's the exact same thing (callbacks, arrgh)] – Max H. Jan 22 '17 at 19:12
  • this is asynchronous, you can get the result with callback : `User.findOne({ _id: id }, function(err, res) { console.log(res); var user = res; });`. If you dont specify callback function it wont execute unless you call exec(), see : http://stackoverflow.com/questions/31549857/mongoose-what-does-the-exec-function-do – Bertrand Martel Jan 22 '17 at 19:16
  • This, for some reason, will only return the boolean which I think resembles `exists`. I may have a pretty obvious error here, but I can't seem to find it right now. Here's the full code of the function: http://pastebin.com/2FCXkrF4 – Max H. Jan 22 '17 at 19:33
  • `user.data = data;console.log(exists);user.save();` must go in the callback of `findOne`, when the user has been found – Bertrand Martel Jan 22 '17 at 19:51