Mongoose JS findOne always returns null
【问题】
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的更多相关文章
- 160304-02、JS 中如何判断null 和undefined
JavaScript 中有两个特殊数据类型:undefined 和 null,下节介绍了 null 的判断,下面谈谈 undefined 的判断. 以下是不正确的用法: var exp = undef ...
- PHP - json_decode returns NULL的解决办法
碰到了PHP json_decode returns NULL, 肿么办? 1. google 一下, 关键字:PHP json_decode NULL 首先你能看到我这个这个帖子:) http:// ...
- js中NAN、NULL、undefined的区别
NaN:保留字(表明数据类型不是数字) undefined:对象属性或方法不存在,或声明了变量但从未赋值.即当你使用了对象未定的属性或者未定义的方法时或当你声明一个变量,但你确从未对其进行赋值,便对其 ...
- JS中undefined和null的区别
在写JS脚本的时候,经常会碰到“为空”的判断,其中主要有null和undefined的判断.这两个为空判断的主要区别是: 1) null是JS的关键字,是语法特性.undefined是全局对象的属性, ...
- JS中undefined与null的有趣 关系
今天学习中遇到了一个有意思的问题. var obj = undefined 我们将一个对象设置为undefined typeof(obj)>>undefined 结果是undefined, ...
- JS中原始类型Null和Undefined
Undefined类型只有一个值,即undefined.当声明的变量还未被初始化时,变量的默认值为undefined.Null类型也只有一个值,即null.null用来表示尚未存在的对象,常用来表示函 ...
- js中使用0 “” null undefined {}需要注意
注意:在js中0为空(false) ,代表空的还有“”,null ,undefined: 如果做判断if(!上面的四种值):返回均为false console.log(!null);// true c ...
- js 中 0 和 null 、"" Boolean 值关系
在做字符串非空判断时,无意发现一个问题,记录下以便以后回顾. 问题描述:非空判断,只是校验传值的内容是否为"".null .undefined.当变量 赋值的字符串内容为 0,此时 ...
- 【学习笔记】js中undefined和null的区别和联系
在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...
随机推荐
- Java中的锁(转)
Java中的锁 锁像synchronized同步块一样,是一种线程同步机制,但比Java中的synchronized同步块更复杂.因为锁(以及其它更高级的线程同步机制)是由synchronized同步 ...
- CAS服务器配置
参考文献: http://sucre.blog.51cto.com/1084905/683624 1.安装部署CAS Server 从官网下载CAS Server,今天发现CAS Server的官网居 ...
- rcp(插件开发)插件B需要引用插件A中的jar包-如何处理依赖关系
如果插件B需要引用插件A中的jar 通常需要以下几步: 1.插件B要依赖插件A 2.在插件B的build path中添加插件A的jar包 3.插件A的runtime导出插件B中使用jar的packag ...
- C# 地磅串口编程
C# 地磅串口编程 http://www.cnblogs.com/cancer_xu/archive/2012/09/14/WeighBridge-Com.html http://www.cnblog ...
- 支持xp风格的manifest
MSDN 和一些网站上的manifest 有问题 , 自己修改了一下加上Microsoft.VC80.DebugCRT 和 Microsoft.VC80.DebugMFC 就可以了.如果是relea ...
- springcloud超时时间与重试次数配置
#hystrix配置hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000ribbon.Conn ...
- 三个实例演示 Java Thread Dump 日志分析
原文地址: http://www.cnblogs.com/zhengyun_ustc/archive/2013/01/06/dumpanalysis.html jstack Dump 日志文件中的线程 ...
- The Topo to Raster tool returns errors 010235 and 010067转
Problem: The Topo to Raster tool returns errors 010235 and 010067 Description The Topo to Raster geo ...
- 【GitLab】gitlab上配置webhook后,点击测试报错:Requests to the local network are not allowed
gitlab上配置webhook后,点击测试报错: Requests to the local network are not allowed 操作如下: 报错: 错误原因: gitlab 10.6 ...
- WordPress主题开发:WP_Query使用分页实例
functions.php加入 <?php function lingfeng_custom_pagenavi( $custom_query,$range = 4 ) { global $pag ...