问题描述
我目前正在建立一个非常小的CRM练习用,我正在尝试创建一个页面,您可以在其中搜索个人资料。我希望用户输入他所了解的有关正在查找的个人资料的所有数据,但不允许他输入填充每个输入。当我尝试从没有1个字段的MongoDB中获取数据时,它返回null,我该怎么办?
name: String,email: String,date: String,notes: String
})
module.exports = mongoose.model('profiles',createProfile)```
this is the schema
await mongo().then( async (mongoose) => {
try {
console.log('Finding data from MongoDB')
const result = await createProfile.findOne({
name: req.body.name,email: req.body.email,notes: req.body.notes
})
console.log(result)
res.redirect('/find')
} catch {
console.log('Can not connect')
} finally {
mongoose.connection.close()
}
})
this is the operation
解决方法
var query = {};
['name','email','notes'].forEach(key => {
if (req.body[key]) {
query[key] = req.body[key];
}
});
const result = await createProfile.findOne(query);
仅包括用户为其提供值的键。