我正在嘗試檢查用戶帳戶是否已在用戶模型或TempUser模型中創建,但是遇到問題,我無法將找到的值添加到對象以處理條件檢查。無法將屬性添加到對象 - Node.js/Mongoose
請檢查下面的代碼和註釋:
exports.isRegistered = function(userModel, tempUserModel, username){
var obj = {}; //creating an empty object to add the found results later
userModel.findOne({username: username}, function(err, user){
if(err) throw err;
if(user){
obj.user = user;
console.log(obj); // doesn't fire as no user was found found
}else{
tempUserModel.findOne({username: username}, function(err, tempUser){
if(err) throw err;
obj.tempUser = tempUser;
console.log(obj); // returns object with a tempUser as it was found
});
}
});
console.log(obj); //returns an empty object again.
};
感謝您的澄清和參考,將嘗試更好地理解一切。 –