在此先感谢您的帮助,非常感谢!
我只是想在数据库中试图 find()
文件 . 为了简洁起见,我现在没有比这更多的事情了):
var mongoose = require('mongoose');
mongoose.connect('mongodb://<username>:<password>@<sub-domain>.mongolab.com:<port>/<db>');
var schema = { email_address: String, invite: String }
, Users = mongoose.model('Users', new mongoose.Schema(schema));
console.log(Users.findOne({ 'email_address': 'jonathon@foo.bar' }, function(err, doc) { return doc; }));
我很确定应该将返回的 doc
回显到Node.js控制台(终端),但我得到的是:
{ options: { populate: {} },
safe: undefined,
_conditions: { email_address: 'jonathon@foo.bar' },
_updateArg: {},
_fields: undefined,
op: 'findOne',
model:
{ [Function: model]
modelName: 'Users',
model: [Function: model],
options: undefined,
db:
{ base: [Object],
collections: [Object],
models: {},
replica: false,
hosts: null,
host: '<sub-domain>.mongolab.com',
port: <port>,
user: '<username>',
pass: '<password>',
name: '<db>',
options: [Object],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
db: [Object] },
schema:
{ paths: [Object],
subpaths: {},
virtuals: [Object],
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
_requiredpaths: undefined,
options: [Object] },
collection:
{ collection: null,
name: 'users',
conn: [Object],
buffer: true,
queue: [Object],
opts: {} },
base:
{ connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: {},
Collection: [Function: NativeCollection],
Connection: [Function: NativeConnection],
version: '3.5.4',
Mongoose: [Function: Mongoose],
Schema: [Object],
SchemaType: [Object],
SchemaTypes: [Object],
VirtualType: [Function: VirtualType],
Types: [Object],
Query: [Object],
Promise: [Function: Promise],
Model: [Object],
Document: [Object],
Error: [Object],
mongo: [Object] } } }
显然我只是用 <username>
位模糊了我的真实凭据,它们在我的代码中都是正确的 .
虽然从 findOne
方法中删除条件,但数据库中确实有一个与条件匹配的文档!
我是Node.js的新手,所以我可以解释你的答案,所以我知道下次它会是一个很好的帮助!谢谢!
2 回答
将您的代码更改为:
我无法从您的代码中看到,但我认为您正在将mongoose函数的值写入控制台...
我完全忘记了,Node.js是异步的,所以行
console.log(Users.findOne({ 'email_address': 'jonathon@foo.bar' }, function(err, doc) { return doc; }));
确实回显到控制台,尽管数据库尚未返回任何文件!相反,
console.log
方法应该在find
的回调中,la: