如何使用mongoose findOne [英] How to use mongoose findOne

查看:717
本文介绍了如何使用mongoose findOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模式(对于它在coffeescript中的道歉)

  Schema = mongoose.Schema 

AuthS = new Schema
auth:{type:String,unique:true}
nick:String
time:Date
Auth = mongoose.model'Auth',AuthS

我只想恢复一个记录,绝对在我的数据库:

  Auth.findOne({nick:'noname'},function(obj){console.log(obj);});不幸的是,这总是记录 null    在mongo shell中的db.auths.findOne({nick:'noname'})总是返回一个值。到底是怎么回事? 

解决方案

发现问题,需要使用 function(err,obj)代替:

  Auth.findOne({nick:'noname'},function(err,obj){console.log obj);}); 


I have the below schema (apologies that it is in coffeescript)

Schema = mongoose.Schema

AuthS = new Schema
    auth:   {type: String, unique: true}
    nick:   String
    time:   Date
Auth = mongoose.model 'Auth', AuthS

I simply want to recover one record which is definitely in my database:

Auth.findOne({nick: 'noname'}, function(obj) { console.log(obj); });

Unfortunately this always logs null. db.auths.findOne({nick: 'noname'}) in mongo shell always returns a value. What is going on?

解决方案

Found the problem, need to use function(err,obj) instead:

Auth.findOne({nick: 'noname'}, function(err,obj) { console.log(obj); });

这篇关于如何使用mongoose findOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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