Reputation: 2952
Thanks in advance for your help on this, it's much appreciated!
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': '[email protected]' }, function(err, doc) { return doc; }));
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: '[email protected]' },
_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.
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!
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!
Upvotes: 1
Views: 1026
Reputation: 2952
I totally forget, Node.js is asynchronous so the line console.log(Users.findOne({ 'email_address': '[email protected]' }, function(err, doc) { return doc; }));
is indeed echoing to the console though no documents have been returned by the DB yet!
Instead the console.log
method should be inside the find
's callback, a la:
Users.find({}, function(err, doc) { console.log(doc); });
Upvotes: 2
Reputation: 38499
Change your code to:
Users.findOne({ 'email_address': '[email protected]' }, function(err, doc) {
console.log(doc);
});
I can't see from your code, but I think you are writing the value of the mongoose function to the console...
Upvotes: 4