为什么下面的代码没有达到if条件?
console.log("returnAnonModel -----", returnAnonModel, "typeof : ", typeof returnAnonModel)
返回
returnAnonModel ----- [ { _id: 57d21d2deaa0c4c8297311a6,
__v: 0,
usefulness: [],
reviews: [] } ] typeof : object
代码:
Anon.find({_id : app.locals.user._id})
.then(function( returnAnonModel){
console.log("returnAnonModel -----", returnAnonModel, "typeof : ", typeof returnAnonModel)
console.log("Object.keys(returnAnonModel).length", Object.keys(returnAnonModel.toObject()).length)
console.log("returnAnonModel.toObject()---", returnAnonModel.toObject())
if(returnAnonModel){
console.log("*************Anon user found*********************");
}else{
console.log("Anon user NOT found--")
}
编辑:我刚尝试了if(returnAnonModel.length){
,但仍然显示if或者其他日志
EDIT2:我把它改成了下面,现在它可以工作了。为什么?是因为我有returnAnonModel
它是控制台2次我知道这次我使用findOne
而不是find
。主要区别是什么?
Anon.findOne({_id : app.locals.user._id})
.then(function( returnAnonModel){
console.log("*******", returnAnonModel ,"***********88")
// console.log("returnAnonModel -----", returnAnonModel, "typeof : ", typeof returnAnonModel)
// console.log("Object.keys(returnAnonModel).length", Object.keys(returnAnonModel.toObject()).length)
// console.log("returnAnonModel.toObject()---", returnAnonModel.toObject())
if(returnAnonModel){
console.log("*************Anon user found*********************");
}else{
console.log("*************Anon user NOT found*************")
}