我想检查嵌入式文档中的元素是否为空。例如:
if (files.originalFilename === 'photo1.png') {
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } }
);
console.log('Got photo1!');
// save the user
user.save();
}
目前,我无法更新字段" photo1"如果是空的如何检查它是否为空?
我尝试使用:
if (user.competitorAnalysisPhoto.photo1 == null) {
console.log('Photo1 is null!');
user.competitorAnalysisPhoto.push({
photo1: files.path
});
} else {
console.log('Photo1 is not null!');
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } }
);
}
但是,无论photo1是否存在,它都是null,打印出来" undefined"。
答案 0 :(得分:1)
您是否尝试使用'upsert'?
if (files.originalFilename === 'photo1.png') {
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } },
{ upsert: true }
);
console.log('Got photo1!');
// save the user
user.save();
}