Cannot read property 'code' of undefined from promise, context api (react hook)

I am making my first hybrid web app using react hook.
I am faced with a timing problem with promise and context api.

Here are the logic I am facing with.

  1. A function fetchApplications gets data from firebase firestore, it is defined in globalcontext.js (called in tableView)
  2. tableView.js calls fetchApplications in useEffect.
  3. fetchApplications is defined as promise function, I expect it will resolve(return) data until data fetched, it will resolove(return) object like {code:200, data:data}

problem
in the fetchData.code,

Cannot read property ‘code’ of undefined

Here is my code

In the tableview.js

  React.useEffect(() => {
      context.fetchApplications(auth.userId, "")
      .then(function (fetchData) {
      console.log("Fetched data: ", fetchData);   ///this is undefined
      if (fetchData.code !== 200) {               /// error part
        alert(fetchData.msg);
      }
      if (!fetchData.data) {
        alert("No applications");
      }
      setsData(fetchData.data);
    });
  }, []);

In the GlobalContext.js

async function fetchApplications(userId, role) {
    return new Promise((resolve, reject) => {
      // resolve({ code: 200, data: "data" });     //If I add this code, it will be alright.

      let dataArray = [];
      let applicationRef = db
        .collection("Users")
        .doc(userId)
        .collection("Applications");
      applicationRef
        .get()
        .then(function (qs) {
          qs.forEach(function (doc) {             //doesn't work
            console.log(doc.id, " => ", doc.data());
            console.log(doc.size, " => ", typeof doc);
            dataArray.push(doc.data());
          });
          return Promise.resolve(dataArray);
        })
        .then((data) => {
          console.log("Global, Fetched Data", dataArray);
          if (data) {
            resolve({ code: 200, data: data });
          }
          return;
        });
    }).catch(function (error) {
      reject({ code: 400, msg: "시스템에러 고객센터에 문의해주세요" });
    });
  }

wrote in codesendbox
If I was write the wrong way of promise, please let me know.
thanks

208 thoughts on “Cannot read property 'code' of undefined from promise, context api (react hook)”

  1. Pingback: canada viagra
  2. Pingback: viagra buy online
  3. Pingback: cialis tadalafil
  4. Pingback: viagra coupon
  5. Pingback: purchase viagra
  6. Pingback: cialis generic
  7. Pingback: viagra samples
  8. Pingback: viagra 800mg price
  9. Pingback: sildenafil 100mg
  10. Pingback: sale cialis
  11. Pingback: canadian viagra
  12. Pingback: viagra sale online
  13. Pingback: ebay cialis
  14. Pingback: viagra coupons
  15. Pingback: sildenafil online
  16. Pingback: viagra connect
  17. Pingback: female viagra
  18. Pingback: levitra
  19. Pingback: roman viagra
  20. Pingback: chewable viagra
  21. Pingback: viagra dosage
  22. Pingback: levitra vs viagra
  23. Pingback: viagra kopen
  24. Pingback: buy viagra cheap
  25. Pingback: low price viagra
  26. Pingback: viagra cialis
  27. Pingback: pfizer viagra
  28. Pingback: natural viagra
  29. Pingback: Anonymous
  30. Pingback: Anonymous
  31. Pingback: Anonymous
  32. Pingback: ivermectin
  33. Pingback: prednisone pills
  34. Pingback: ivermectin canada
  35. Pingback: order viagra
  36. Pingback: otc sildenafil
  37. Pingback: stromectol kaufen
  38. Pingback: cialis dosis
  39. Pingback: cialis bula
  40. Pingback: viagra for covid
  41. Pingback: generic
  42. Pingback: ivermectin can
  43. Pingback: cheap cialis india
  44. Pingback: merck ivermectin
  45. Pingback: generic
  46. Pingback: cialis goodrx
  47. Pingback: ivermectin canada
  48. Pingback: ivermectin covid
  49. Pingback: stromectol pills
  50. Pingback: tadalafil walmart
  51. Pingback: ivermectin 9mg
  52. Pingback: ivermectine kory
  53. Pingback: iwermektyna
  54. Pingback: ivermectin uk buy
  55. Pingback: ivermectin 3mg tab