【问题】

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. 10 个理由让你继续干 IT

    1.钱,钱,钱 对,我们努力工作就是为了赚钱,而IT专业人士的努力工作的确得到了很好的补偿.报酬不仅仅是好而已,而是非常棒.根据美国劳工部<2010年美国 就业与报酬情况概览>(表6,PD ...

  2. HDU 3472 HS BDC (混合图的欧拉路径判断)

    HS BDC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. [置顶] android socket 聊天实现与调试

    网上很多基于Socket的聊天实现都是不完整的... 结合自己的经验给大家分享一下,完整代码可以在GitHub里获取https://github.com/zz7zz7zz/android-socket ...

  4. ASP.NET Web API接受AngualrJS的QueryString的两种方式

    ASP.NET Web API如何接受来自AngualrJS的QueryString呢?本篇体验两种方式. 第一种方式:http://localhost:49705/api/products?sear ...

  5. C#编程(四十五)----------格式字符串

    格式字符串 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) 案例: string str=string.Format("{0:C}",0.2); ...

  6. Linux学习4-阿里云服务器(CentOS)下使用 Tomcat安装 Jenkins

    前言 通常做自动化测试,需要用到jenkins来做持续集成,那么linux系统里面如何使用tomcat安装Jenkins环境呢? 前面一篇已经搭建好java和tomcat环境,接着直接下载jenkin ...

  7. Spring+Velocity(平台升级至Spring Framework 5.0.2)

    下载: http://repo.spring.io/release/org/springframework/spring/ Dear Spring community, I’m pleased to ...

  8. go 用的不多的命令

    8.go doc 文档注释相关,可以搭建本地GO文档服务器,包含自己的项目注释,更多细节请参考:https://github.com/hyper-carrot/go_command_tutorial/ ...

  9. 详细解读Volley(三)—— ImageLoader & NetworkImageView

    ImageLoader是一个加载网络图片的封装类,其内部还是由ImageRequest来实现的.但因为源码中没有提供磁盘缓存的设置,所以咱们还需要去源码中进行修改,让我们可以更加自如的设定是否进行磁盘 ...

  10. Material Designer的低版本兼容实现(十四)—— CardView

    今天说的又是一个5.0中才有的新控件——CardView(卡片视图).这个东东其实我们早就见过了,无论是微博还是人人客户端,它都有出现.通常我们都是通过自定义一个背景图片,然后通过给layout进行设 ...