在 Node.js 中使用 Mongoose 在 MongoDB 中查找文档时出现奇怪的响应 [英] Strange response when finding documents in MongoDB using Mongoose in Node.js
问题描述
在此先感谢您的帮助,非常感谢!
Thanks in advance for your help on this, it's much appreciated!
我在一个新项目中使用 Node.js、MongoDB 和 Mongoose,我只是想find()
数据库中的文档.为简洁起见,我将只包含下面的 Mongoose 代码(无论如何我现在不会做更多的事情):
I'm using Node.js, MongoDB and Mongoose in a new project and I'm simply trying to find()
the documents in a database. For brevity I'll just include the Mongoose code below (I'm not doing much more than this anyway right now):
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 控制台(终端),但我得到了这个:
I'm quite sure that should just echo the returned doc
to the Node.js console (Terminal) but instead I'm getting this:
{ 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] } } }
显然,我只是用
位混淆了我的真实凭证,它们在我的代码中都是正确的.
Obviously I've just obfuscated my real credential with the <username>
bits, they are all correct in my code.
数据库中确实有一个与条件匹配的文档,尽管即使从 findOne
方法中删除条件也不会产生任何结果!
The database does have a document in it that would match the condition though even removing the condition from the findOne
method yields no results!
我对 Node.js 还很陌生,所以我可以解释你的答案,所以我知道下次它会很有帮助!谢谢!
I'm fairly new to Node.js so I you could explain your answers so I know for next time it'd be a great help! Thanks!
推荐答案
我完全忘记了,Node.js 是异步的,所以 console.log(Users.findOne({ 'email_address': 'jonathon@foo.bar' }, function(err, doc) { return doc; }));
确实在回显到控制台,尽管 DB 还没有返回任何文档!
I totally forget, Node.js is asynchronous so the line console.log(Users.findOne({ 'email_address': 'jonathon@foo.bar' }, function(err, doc) { return doc; }));
is indeed echoing to the console though no documents have been returned by the DB yet!
相反,console.log
方法应该在 find
的回调中,例如:
Instead the console.log
method should be inside the find
's callback, a la:
Users.find({}, function(err, doc) { console.log(doc); });
这篇关于在 Node.js 中使用 Mongoose 在 MongoDB 中查找文档时出现奇怪的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!