在mongoDB中搜索集合时,我如何接受空数据



我目前正在为实践构建一个非常小的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);

只包括用户提供值的键。

最新更新