Home > Net >  Error when trying to push something in mongodb
Error when trying to push something in mongodb

Time:06-02

code

      case 'setup':
        let serverSetup = ServerDoc.findOne({id: message.guild.id})
        if(serverSetup) return console.log(serverSetup)
        if(!serverSetup){
          let newServersetup = new ServerDoc({name: message.guild.name})
           newServersetup.save();
             console.log(newServersetup)
        }   

error https://pastecord.com/zutetugymo because I couldn't paste here

CodePudding user response:

What you think is your error is actually the result of the ServerDoc.findOne(). Since you are not using await or .then() when you are finding a document from your collection, this is what you get when you use .findOne(). To actually get the data instead of this, you just have to change your code to this:

case 'setup':
    let serverSetup = await ServerDoc.findOne({id: message.guild.id})
    if(serverSetup) return console.log(serverSetup)
    if(!serverSetup){
        let newServersetup = new ServerDoc({name: message.guild.name})
        newServersetup.save();
        console.log(newServersetup)
    } 
  • Related