Mongoose TypeError on a model's findOne method

前端 未结 1 1332
北荒
北荒 2021-01-03 00:52

The following TypeError cropped up in some old code.

TypeError: Object # has no method \'findOne\'


The Model that was affecte

相关标签:
1条回答
  • 2021-01-03 01:41

    It's known node.js issue. It means that you have looping require somewhere in your code and node.js forbids it.

    The right way to do it is by using mongoose.model method. So, instead of UserModel variable you shall use mongoose.model('UserModel'). So, when findSomeUsers will be called mondoose will fetch UserModel and invoke its find method.

    GroupSchema.statics.findSomeUsers = function(group, callback) {
        this.find({name : session_user._id}, function(err, groups) {
            mongoose.model('UserModel').find({_id : {$in : group.users}}, function(err,patients) {
                // do magic
            });
        });
    };
    
    0 讨论(0)
提交回复
热议问题