I am currently building a very small CRM for practice and i'm trying to make a page you can search profiles in. i want the user to enter all the data he knows about the profile he is looking up but allow him not to fill every input. When i tried to fetch data from MongoDB without 1 field it returned null, what can i do about it?
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);
Only include the keys that the user provides a value for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.