[英]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.