【发布时间】:2021-04-04 00:52:30
【问题描述】:
这是我得到的错误 => "TypeError: apartment.lean is not a function"
在这段代码中,我收到一条错误消息,提示lean() 不是函数:
let apartment = await Apartment.findOne({
'address.placeId':req.body.placeId,
'address.apartmentNumber':req.body.apartmentNumber
});
if(apartment){
//fetch references of pictures belonging to the apartment and send them back to the user
const pictureReferences = fetchPictureReferences(apartment._id);
let apartmentPoJo = apartment.lean();
apartmentPoJo.pictures=[...pictureReferences];
return res.status(200).json({msg:'Apartment Found',apartment:apartmentPoJo});
}
在该代码中,它运行良好,基本上是在 findOne() 完成后立即调用lea() 函数:
let apartment = await Apartment.findOne({
'address.placeId':req.body.placeId,
'address.apartmentNumber':req.body.apartmentNumber
}).lean();
if(apartment){
//fetch references of pictures belonging to the apartment and send them back to the user
const pictureReferences = fetchPictureReferences(apartment._id);
apartment.pictures=[...pictureReferences];
return res.status(200).json({msg:'Apartment Found',apartment});
}
你能告诉我为什么它们不同吗?我假设 findOne() 函数返回一个文档。是当场执行还是在我创建的新变量上执行?
感谢您的澄清!
【问题讨论】: