dimanche 30 mai 2021

if statement doesn't works with socket.io and mongodb

i want to check if some values on my mongodb database exist, but if not exist, give me an error, instead of a event.
This is my code:

   socket.on("bot_req_id", async function(data) {
       let db = mongoose.db("wumpusCave")
       let bots = db.collection("bots")
       let find = bots.find({data})
       if(find) {
           console.log(data)
           let bot = await bots.findOne({"id":data})
           console.log(bot)
           socket.emit('bot_res_id', {"name": bot.name, "description": bot.description, "avatar": bot.avatar, "id": bot.id, "invite": bot.invite})
       } else {
           socket.emit('notfound', "bot non trovato")
       }
   })

The error:

           socket.emit('bot_res_id', {"name": bot.name, "description": bot.description, "avatar": bot.avatar, "id": bot.id, "invite": bot.invite})
                                                  ^

TypeError: Cannot read property 'name' of null

I use node.js
How i can fix that?
Thanks in advice and sorry for bad english!

Aucun commentaire:

Enregistrer un commentaire