【问题】

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);

});

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

Error: null

User: null

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?

Thanks in advance.

 

【答案】

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.

 

来自: https://stackoverflow.com/questions/28777907/mongoose-js-findone-always-returns-null

 

Mongoose JS findOne always returns null的更多相关文章

  1. 160304-02、JS 中如何判断null 和undefined

    JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的用法: var exp = undef ...

  2. PHP - json_decode returns NULL的解决办法

    碰到了PHP json_decode returns NULL, 肿么办? 1. google 一下, 关键字:PHP json_decode NULL 首先你能看到我这个这个帖子:) http:// ...

  3. js中NAN、NULL、undefined的区别

    NaN:保留字(表明数据类型不是数字) undefined:对象属性或方法不存在,或声明了变量但从未赋值.即当你使用了对象未定的属性或者未定义的方法时或当你声明一个变量,但你确从未对其进行赋值,便对其 ...

  4. JS中undefined和null的区别

    在写JS脚本的时候,经常会碰到“为空”的判断,其中主要有null和undefined的判断.这两个为空判断的主要区别是: 1) null是JS的关键字,是语法特性.undefined是全局对象的属性, ...

  5. JS中undefined与null的有趣 关系

    今天学习中遇到了一个有意思的问题. var obj = undefined 我们将一个对象设置为undefined typeof(obj)>>undefined 结果是undefined, ...

  6. JS中原始类型Null和Undefined

    Undefined类型只有一个值,即undefined.当声明的变量还未被初始化时,变量的默认值为undefined.Null类型也只有一个值,即null.null用来表示尚未存在的对象,常用来表示函 ...

  7. js中使用0 “” null undefined {}需要注意

    注意:在js中0为空(false) ,代表空的还有“”,null ,undefined: 如果做判断if(!上面的四种值):返回均为false console.log(!null);// true c ...

  8. js 中 0 和 null 、"" Boolean 值关系

    在做字符串非空判断时,无意发现一个问题,记录下以便以后回顾. 问题描述:非空判断,只是校验传值的内容是否为"".null .undefined.当变量 赋值的字符串内容为 0,此时 ...

  9. 【学习笔记】js中undefined和null的区别和联系

    在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...

随机推荐

  1. 大型电商业务架构 IT大咖说 - 大咖干货,不再错过

    大型电商业务架构 IT大咖说 - 大咖干货,不再错过 http://www.itdks.com/dakashuo/new/dakalive/detail/591

  2. IAR EWARM : Debugging with CMSIS-DAP

  3. spy++使用指南

    很多朋友都对窗口句柄比较迷糊,这篇短文就以spy++这个软件为主,介绍下窗体句柄和使用按键插件时,如果对这个句柄发送消息,即所谓的后台挂机.spy++这个软件来自VC++,装好VC后,就可以在工具中看 ...

  4. [Go] defer 语句

    Go 还有一些特有的流程控制语句,其中一个就是 defer 语句.该语句用于延迟调用指定的函数,它只能出现在函数的内部,由 defer 关键字以及针对某个函数的调用表达式组成.这里被调用的函数称为 延 ...

  5. Introduction to the Optimizer --cbo

    http://docs.oracle.com/cd/B10500_01/server.920/a96533/optimops.htm

  6. Java嵌入式数据库H2学习总结(三)——在Web应用中嵌入H2数据库

    H2作为一个嵌入型的数据库,它最大的好处就是可以嵌入到我们的Web应用中,和我们的Web应用绑定在一起,成为我们Web应用的一部分.下面来演示一下如何将H2数据库嵌入到我们的Web应用中. 一.搭建测 ...

  7. CentOS RabbitMQ 高可用(Mirrored)

    原文:https://www.sunjianhua.cn/archives/centos-rabbitmq.html 一.RabbitMQ 单节点 1.1.Windows 版安装配置 1.1.1 安装 ...

  8. Control reaches end of non-void function 犯过最傻的错误

    之所以会报“Control reaches end of non-void function ”的警告,时因为方法名中缺少返回类型.正确的写法如下: +(void)setMobile:(NSStrin ...

  9. 组织安全性SQL

    BEGIN mo_global.set_policy_context ('S', '&组织ID'); END; SELECT * FROM hr_organization_units_v;   ...

  10. 从零开始写一个发送h264的rtsp服务器(上)

    转自:http://blog.csdn.net/jychen105/article/details/47006345 一.什么是RTSP 通常所说的rtsp协议其实包含三个协议: rtsp协议, rt ...