gpt4 book ai didi

javascript - 我的 UserModel 没有返回正确的用户数据查找 _id?

转载 作者:行者123 更新时间:2023-12-03 03:47:09 27 4
gpt4 key购买 nike

这是我的代码:

var user = UserModel.findOne({ _id: decodedToken.id, }, function (err, user) {
if (err) return handleError(err)
console.log('inside UserModel user = ', user) // this is what I want to return on the variable user
});

console.log("Outside userModel = ",user) // the user inside the findOne callback isn't assigned on the variable user?

在 UserModel.findOne 回调中,它返回正确的用户:

{ _id: 5979744a02bcec1dc873a96c,
updatedAt: 2017-07-27T05:04:10.313Z,
createdAt: 2017-07-27T05:04:10.313Z,
fullName: 'kring kring',
email: 'sofbaculio@gmail.com',
password: '$2a$08$yiDO.HBGoieBApY8VCHA/Opp6PEpq7.CwZmfc2CkQmQoRfB/ySi4u',
__v: 0,
active: true }

但在回调之外它会返回如下内容:

 Query {
_mongooseOptions: {},
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
opts: { bufferCommands: true, capped: false },
name: 'user',

帮忙?

最佳答案

那是因为您的函数实际上返回一个 Mongoose 查询,您稍后可以使用它来调用 exec 方法并使用它来获取结果。

您现在需要执行该查询才能获取结果:

user.exec(function(err,result){
if(err)
return console.log(err);
console.log(result);
});

您可以阅读有关使用 Mongoose 查询的更多信息 @ http://mongoosejs.com/docs/queries.html

关于javascript - 我的 UserModel 没有返回正确的用户数据查找 _id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45342146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com