[英]How can i get and display all value without undefined value from mongodb?
我如何从 mongodb 获取并显示所有没有未定义值的值?
app.post('/add_teacher',(req,res)=>{
const name = req.body.name;
const biography = req.body.biography;
teacherCollection.insertOne({name,biography})
.then(result =>{
console.log(result);
res.send(result.insertedCount>0);
})
})
app.get('/get_all_teacher', (req, res) =>{
teacherCollection.find()
.toArray((err, course) =>{
res.send(course);
})
})
假设,我给名字一个值,但不给传记值。所以传记的值将是“未定义的”。现在我不想在值是“未定义的”的地方得到任何东西。我用过 mongodb
在javascript
,当您想要从object
访问key
并且该key
在object
中不存在时,它返回undefiend
const biography = req.body.biography; // return undefiend if not exists
如果您想要null
值或key
不存在时的default
值,此代码将帮助您
const biography = req.body.biography || null; // return null if not exists
const biography = req.body.biography || ''; // return empty string if not exists
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.