Get async value from firestore

前端 未结 3 1164
无人及你
无人及你 2021-01-06 15:35

I am struggling with async operations. I am trying to simply get a value from firestore and storing it in a var.

I manage to receive the value, I can even save it in

3条回答
  •  太阳男子
    2021-01-06 16:23

    Finally managed to get it working. Thanks for the input Tomalak!

    getValues(help.collectionName, help.docName)
      .then((text) => {
        console.log(text);
        help.message = text;
       })
      .catch((err) => { console.log("Error: ", err); });
    
    function getValues(collectionName, docName) {
      return db.collection(collectionName).doc(docName).get().then((doc) => {
        if (doc.exists) {
          return doc.data().text;
        }
        else {
          return Promise.reject("No such document");
        }});
      }
    
    bot.help((ctx) => ctx.reply(help.message));
    

    Unfortunately, I can not pin-point the exact reason this worked. Some little fixes (missed comma in the console.log) and formatting definitely helped me understanding the structure though. Hope someone else finds this useful, when starting to play around with node and firebase.

提交回复
热议问题