1

I'm trying to figure out how to load a user based on ID with mongoose in a node application. I come from a php/mysql background and I'm having a hard time figuring out how to tie this all together in node+mongoose. Can someone help me to improve/fix this example?

Model models/user.js

var mongoose = require('mongoose')
    , Schema = mongoose.Schema;

var userSchema = new Schema({
    facebookProfile: {},
    facebookPages: { type: Array },
    created: {type: Date, default: Date.now}
});

userSchema.methods = {
    getManagedPages: function(){
        return this.facebookPages;
    }
}

userSchema.statics = {
    load: function(userid){
            return this.findById(userid);
    }
}
module.exports = mongoose.model('User', userSchema);

in the controller controllers/user.js

var User = mongoose.model("User");
var currentUser = new User();
currentUser = currentUser.load(req.user._id);

The error I'm getting is:

TypeError: Object { _id: 52861322f38d7ded3f000001, created: Fri Nov 15 2013 13:27:14 GMT+0100 (CET), facebookPages: [] } has no method 'load' at exports.profile

Jorre
  • 17,273
  • 32
  • 100
  • 145

1 Answers1

1

Check Mongoose Queries and add this in your User model

exports.load(userid, callback){
    var query = User.findOne( {//User is your mongoose.model("User"); variable
            'userid' : userid
        });
        query.select('userId name email');
        query.exec(function(err, result) {
            if (err) {
                // Error
                        callback(err);
            } else {
                if (result.length == 0) {
                    // User not Exists              
                        callback(false, result);
                }
                else {
                    // User Exists. Process result
                }
            }
        });
}

EDIT: Updated code with callback

Change currentUser = currentUser.load(req.user._id); to a callback function

currentUser.load(req.user._id, function(err,result){
   if(err){//handle err
   }
   else{
    currentUser = result.userid;
   }
});

Note:It is pseudocode

Damodaran
  • 10,882
  • 10
  • 60
  • 81
  • Thanks, I can get my user based on that syntax, but I don't really like having all that database code in my controllers and would like to move it to my models, for example in a method called "load" as you can see in my above example. Do you know how I can move that logic to the "load" function in my User model as you can see above? – Jorre Nov 15 '13 at 12:33
  • Shouldn't we be using statics or functions instead on the model (http://mongoosejs.com/docs/2.7.x/docs/methods-statics.html) or should I export every function manually like in your example? – Jorre Nov 15 '13 at 12:48
  • Ya you can use static function. I just provided the basic structure – Damodaran Nov 15 '13 at 12:59
  • I know, thanks a lot for that, the code works.However, I'm looking for a way to make it work from the model, by using statics or functions, like in my original question above. Is there a way you can tell me how to do so? I'll mark your answer as is, but it doesn't clarify why a static or function doesn't work in the model and that's exactly what I'd like to solve with my question. I hope you understand! Thanks for your help, it clarified a lot already. – Jorre Nov 15 '13 at 13:25
  • check these links 1) http://stackoverflow.com/questions/14277518/how-to-access-a-static-method-from-a-instance-method-in-mongoose 2) http://stackoverflow.com/questions/8987851/creating-methods-to-update-save-documents-with-mongoose – Damodaran Nov 15 '13 at 14:02
  • Here is a good tutorial that I used to refer: http://blog.nodeknockout.com/post/9056394406/countdown-to-ko-6-getting-started-with-mongoose and one more link in SO: http://stackoverflow.com/questions/16663072/typeerror-on-static-method-of-mongoose-model – Damodaran Nov 15 '13 at 14:07
  • I not used static methods and so i am interested in your question. Let me know how you solved it. – Damodaran Nov 15 '13 at 14:09