简体   繁体   中英

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?

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

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

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

const biography = req.body.biography || null; // return null if not exists

const biography = req.body.biography || ''; // return empty string if not exists

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM