簡體   English   中英

我如何從 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);
  })
})

假設,我給名字一個值,但不給傳記值。所以傳記的值將是“未定義的”。現在我不想在值是“未定義的”的地方得到任何東西。我用過 mongodb

https://i.stack.imgur.com/39qHH.jpg

javascript ,當您想要從object訪問key並且該keyobject中不存在時,它返回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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM