Mongoose JS findOne always returns null

前端 未结 1 1975
逝去的感伤
逝去的感伤 2020-12-11 02:54

I\'ve been fighting with trying to get Mongoose to return data from my local MongoDB instance; I can run the same command in the MongoDB shell and I get results back. I have

相关标签:
1条回答
  • 2020-12-11 03:47

    Mongoose pluralizes the name of the model as it considers this good practice for a "collection" of things to be a pluralized name. This means that what you are currently looking for in code it a collection called "users" and not "user" as you might expect.

    You can override this default behavior by specifying the specific name for the collection you want in the model definition:

    var userModel = mongoose.model('user', userSchema, 'user');
    

    The third argument there is the collection name to be used rather than what will be determined based on the model name.

    0 讨论(0)
提交回复
热议问题