[英]How can i get and display all value without undefined value from mongodb?
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);
})
})
suppose,i give a value for name but don't give value for biography.so the value of biography will be "undefined".now i want to get nothing where value is "undefined".I have used mongodb假设,我给名字一个值,但不给传记值。所以传记的值将是“未定义的”。现在我不想在值是“未定义的”的地方得到任何东西。我用过 mongodb
https://i.stack.imgur.com/39qHH.jpg https://i.stack.imgur.com/39qHH.jpg
In javascript
when you want to access a key
from an object
and the key
is not exists in object
it return undefiend
在
javascript
,当您想要从object
访问key
并且该key
在object
中不存在时,它返回undefiend
const biography = req.body.biography; // return undefiend if not exists
if you want null
value or a default
value when the key
is not exists, this code will help you如果您想要
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.