這是我的代碼:我的UserModel沒有返回正確的用戶數據查找_id?
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: '[email protected]',
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',
幫幫我?
它仍然是一樣的,你可以用我的代碼做出答案?我在查詢下面嘗試了user.exec(),仍然無法正常工作。 –
您運行exec時是否收到錯誤消息?它輸出到控制檯的是什麼? –
沒有錯誤,但它返回: 無極{ 發射器: EventEmitter { 域名:空, _events:{}, _eventsCount:0, _maxListeners:未定義}, 發出:{}, 結束:虛假} –