【问题】

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. Linux进程管理工具 Supervisord 的安装 及 入门教程

    Supervisor是一个进程管理工具,官方的说法: 用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...

  2. delphi 线程的应用 和spcomm的应用

    http://bbs.csdn.net/topics/390744417 串口控件本身的线程不是这样理解的,你不用管它本身用不用线程,它的内部线程和你也没关系.前面说过了,你可以在自己的主线程里创建好 ...

  3. TStream实现多表提交

    TStream实现多表提交 function TynFiredac.SaveDatas(const ATableName, ATableName2: string; ADeltas: TStream; ...

  4. Xcode 5中非常期待的6个功能

    这里是新特征汇总博文链接:iOS7新特征汇总 小引: 自从北京时间2013年06月11日苹果发布Xcode 5 Developer Preview 1,到现在(2013年7约15日)已经过去一个月,苹 ...

  5. ASP.NET MVC:some benefits of asp.net mvc

    Full control over HTML Full control over URLs Better separation of concerns Extensibility Testabilit ...

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

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

  7. log4j1修改DailyRollingFileAppender支持日志最大数量

    配置说明: log4j.appender.logfile=org.apache.log4j.MyDailyRollingFileAppender log4j.appender.logfile.File ...

  8. springboot redis多数据源设置

    遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库.开发库和线上库. 项目使用的是spring boot集成redis,实现如下: 1. 引入依赖 <dependency> ...

  9. mysql查询当前系统时间

    第一种方法:select current_date: MySQL> select current_date as Systemtime;+------------+| Systemtime |+ ...

  10. DES、MD5、RSA加密算法

    本篇主要是实现标题中的三个加密算法,至于机制大家自行百度吧. 一.DES 实现类:DES.java package com.kale.des; import java.security.SecureR ...