node.jsmongoosepopulate

MissingSchemaError: Schema hasn't been registered for model "Users" when populate


I write model for create User and following and followers

    var UserSchema = new Schema({
created_at : { type: Date, default: Date.now }
, name : String
,   hashedPass : String
,   salt: String
,   email : Email
,   avatar : {type: String, default: "images/default_profile.png" }
,   statuses_count : Number
,   screen_name : String
,   location : String
,   about : String
,   followers: [{ type: ObjectId, ref: 'Users' }]
,   following: [{ type: ObjectId, ref: 'Users' }]
,   followers_count : Number
,   following_count : Number
});

and in create follow in user I do

UserSchema.statics.createFollowing = function(data, callback) {
    console.log("data: ", data);
    this.findOne({
        name : data.name
    }, function(err, docs) {
        if (err)
            callback("error " + err)
        else if (docs) {
            docs.following.push({"_id" : data.name_follow});                
            docs.save(function(err) {                       
                            if (err)
                                callback(err);
                            console.log("___cxxx_____", this);
                            mongoose.model('User').findOne({
                                    _id : docs._id
                                }).populate('following', 'name screen_name avatar').exec(function(err, docs) {
                                    if (err)
                                        callback(err);
                                    console.log('The creator is %s', docs.following)
                                    callback(docs);
                                });
                        }); 
        }
    });
}

when I request Like this

http://localhost:3000/auth/createfollowing?callback=jQuery16104670869295950979_1361607659796&{"name":"testuser1","name_follow":"envatowebdev","token":"ece20af054339bb52d4feee9a05ab8cc401f8dec6b622b8df1e4c0d1789825bee8dd47f572cdc737a098a5be03044596","device":"xphone"}&_=1361607667778

I return like that

    Mongoose: users.findOne({ _id: ObjectId("5128781fcb8c95909c00000e") }) { fields: undefined, safe: undefined }  

/data/workspace/node/serverjs/node_modules/mongoose/lib/utils.js:397
        throw err;
              ^
MissingSchemaError: Schema hasn't been registered for model "Users".
Use mongoose.model(name, schema)
    at NativeConnection.Connection.model (/data/workspace/node/serverjs/node_modules/mongoose/lib/connection.js:597:11)
    at model.populate [as _populate] (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:150:23)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:269:14)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at next (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:232:57)
    at init (/data/workspace/node/serverjs/node_modules/mongoose/lib/model.js:215:12)

Why is it like that, and how can I populate following and followers?


Solution

  • problem is Here model is User but I assigned Users followers: [{ type: ObjectId, ref: 'Users' }]