samedi 27 janvier 2018

Cannot read property of null in mongoose

I want to save my users and get their token after saving and after if block. In First time I have This error: TypeError: Cannot read property 'tok' of null but in Secend Try I Can See users Token and is okey.

How Can I Get users token in First Time? I mean Save Them in if Block and Get Their Token After If Block without null Error? (I added Async But Not Work)

Code:

  User.findById(msg.chat.id)
    .then(async (doc) => {
      if (!doc) { // Isn't in DB   First I Saved Users
        kitty.save()
          .then(async () => {
            await console.log('User Saved');
          }).catch((err) => {
            if (err) {
              console.log(err);
            }
          });
      }

      const itok = await doc.tok;  // I Need Get Their Token here After Saving
      console.log(itok);
    }).catch((err) => {
      if (err) {
        console.log(err);
      }
    });
});

Aucun commentaire:

Enregistrer un commentaire