Jac*_*ury 2 mongoose mongodb node.js mongoose-schema
我正在尝试在数据库上运行mongoose.findOne,但是得到了意外的结果。我的查询是
const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array })
exports.findUser = function findUser(email){
const foundUser = User.findOne({email: email}, function(err, userObj){
if(err){
return err
} else if (userObj){
return userObj
} else{
return null
}
})
return foundUser
}
Run Code Online (Sandbox Code Playgroud)
但是,这将返回以下数据(看似随机?),并且没有我请求的任何数据
Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: null,
opts: { bufferCommands: true, capped: false },
name: 'users',
collectionName: 'users',
conn:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'ds113938.mlab.com',
port: 13938,
user: 'root',
pass: 'root',
name: 'users',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] },
queue: [],
buffer: true,
emitter:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined } },
model:
{ [Function: model]
hooks: Kareem { _pres: {}, _posts: {} },
base:
Mongoose {
connections: [Object],
plugins: [],
models: [Object],
modelSchemas: [Object],
options: [Object] },
modelName: 'User',
model: [Function: model],
db:
NativeConnection {
base: [Object],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
hosts: null,
host: 'ds113938.mlab.com',
port: 13938,
user: 'root',
pass: 'root',
name: 'users',
options: [Object],
otherDbs: [],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
_listening: false,
db: [Object] },
discriminators: undefined,
schema:
Schema {
obj: [Object],
paths: [Object],
subpaths: {},
virtuals: [Object],
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [Object],
_indexes: [],
methods: {},
statics: {},
tree: [Object],
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
query: {},
childSchemas: [],
s: [Object],
options: [Object],
'$globalPluginsApplied': true },
collection:
NativeCollection {
collection: null,
opts: [Object],
name: 'users',
collectionName: 'users',
conn: [Object],
queue: [],
buffer: true,
emitter: [Object] },
Query: { [Function] base: [Object] },
'$__insertMany': [Function],
insertMany: [Function] },
schema:
Schema {
obj:
{ name: [Function: String],
email: [Function: String],
passwordHash: [Function: String],
validation: [Function: String],
validationCode: [Function: String],
favorites: [Function: Array] },
paths:
{ name: [Object],
email: [Object],
passwordHash: [Object],
validation: [Object],
validationCode: [Object],
favorites: [Object],
_id: [Object],
__v: [Object] },
subpaths: {},
virtuals: { id: [Object] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [ [Object], [Object], [Object], [Object] ],
_indexes: [],
methods: {},
statics: {},
tree:
{ name: [Function: String],
email: [Function: String],
passwordHash: [Function: String],
validation: [Function: String],
validationCode: [Function: String],
favorites: [Function: Array],
_id: [Object],
id: [Object],
__v: [Function: Number] },
_requiredpaths: undefined,
discriminatorMapping: undefined,
_indexedpaths: undefined,
query: {},
childSchemas: [],
s: { hooks: [Object], kareemHooks: [Object] },
options:
{ retainKeyOrder: false,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
'$globalPluginsApplied': true },
op: 'findOne',
options: { retainKeyOrder: false },
_conditions: { email: 'Adam@gmail.com' },
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection:
NodeCollection {
collection:
NativeCollection {
collection: null,
opts: [Object],
name: 'users',
collectionName: 'users',
conn: [Object],
queue: [],
buffer: true,
emitter: [Object] },
collectionName: 'users' },
_traceFunction: undefined,
_castError: null,
_count: [Function],
_execUpdate: [Function],
_find: [Function],
_findOne: [Function],
_findOneAndRemove: [Function],
_findOneAndUpdate: [Function] }
Run Code Online (Sandbox Code Playgroud)
我想知道如何解决此问题,这似乎是我尝试运行的查询的概述,而不是所述查询的结果
尝试这个:
const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array })
exports.findUser = function findUser(email, callback){
User.findOne({email: email}, function(err, userObj){
if(err){
return callback(err);
} else if (userObj){
return callback(null,userObj);
} else {
return callback();
}
});
}
Run Code Online (Sandbox Code Playgroud)
当执行User.findOne时,猫鼬会调用mongodb并在回调中返回您的用户(findOne函数中的最后一个参数),以便可以返回找到的用户调用回调。
要调用该findUser函数,您需要传递一个回调,如下所示:
findUser('adam@gmail.com', function(error, userFound) {
console.log(userFound);
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关Mongoose findOne的更多详细信息,并了解回调函数,可以在此处查看
| 归档时间: |
|
| 查看次数: |
2645 次 |
| 最近记录: |