Mongoose JS findOne总是返回null [英] Mongoose JS findOne always returns null

查看:1031
本文介绍了Mongoose JS findOne总是返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让Mongoose从我当地的MongoDB实例返回数据;我可以在MongoDB shell中运行相同的命令,然后我得到结果。我在stackoverflow上发现了一篇关于我所遇到的确切问题的帖子这里;我已经按照这篇文章的答案,但我仍然无法让它工作。我创建了一个简单的项目来尝试简单的工作,这里是代码。

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 found a post on stackoverflow that talks about the exact problem I'm having here; I've followed the answers on this post but I still can't seem to get it working. I created a simple project to try and get something simple working and here's the code.

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

var userSchema = new Schema({
    userId: Number,
    email: String,
    password: String,
    firstName: String,
    lastName: String,
    addresses: [
        {
            addressTypeId: Number,
            address: String,
            address2: String,
            city: String,
            state: String,
            zipCode: String
        }
    ],
    website: String,
    isEmailConfirmed: { type: Boolean, default: false },
    isActive: { type: Boolean, default: true },
    isLocked: { type: Boolean, default: false },
    roles: [{ roleName: String }],
    claims: [{ claimName: String, claimValue: String }]
});

var db = mongoose.connect('mongodb://127.0.0.1:27017/personalweb');
var userModel = mongoose.model('user', userSchema);

userModel.findOne({ email: 'test@test.com' }, function (error, user) {
    console.log("Error: " + error);
    console.log("User: " + user);
});

以下是2个console.log语句的响应:

And here is the response of the 2 console.log statements:

错误:null

用户:null

当调用connect方法时看到与我的Mongo实例的连接,但是当发出findOne命令时,似乎没有任何事情发生。如果我通过MongoDB shell运行相同的命令,我会将用户记录返回给我。有什么我做错了吗?

When the connect method is called I see the connection being made to my Mongo instance but when the findOne command is issued nothing appears to happen. If I run the same command through the MongoDB shell I get the user record returned to me. Is there anything I'm doing wrong?

提前致谢。

推荐答案

Mongoose将模型的名称复数化,因为它认为这是一个收集事物的好习惯,是一个复数名称。这意味着你目前在代码中寻找的是一个名为users的集合而不是你所期望的user。

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.

这篇关于Mongoose JS findOne总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆