Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node.js TypeError: admin.firestore(…).collection(…).doc(…).get(…).data is not a function

I am trying to get a url property of a document by first running an if function to see whether another document has certain property

const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp();

exports.writeToClay = functions
    .region("europe-west1")
    .firestore
    .document("/reviews/{documentId}")
    .onWrite((change, context) => {
      // Grab the current value of what was written to Firestore.
      const websiteId = change.before.data().websiteId;
      console.log("websiteID:", websiteId);
      if (change.after.data().code == "1") {
        const url = admin.firestore().collection(
            "websites"
        ).doc(websiteId).get().data().url;
        console.log("url:", url);
      }
    });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The get() returns a Promise and does not have data() method on it. Try refactoring the code as shown below and handle the promise:

exports.writeToClay = functions
  .region("europe-west1")
  .firestore
  .document("/reviews/{documentId}")
  .onWrite(async (change, context) => { // <-- async function
    const websiteId = change.before.data().websiteId;
    console.log("websiteID:", websiteId);

    if (change.after.data().code == "1") {
      // add await here
      const snap = await admin.firestore().collection("websites").doc(websiteId).get()
      const url = snap.data().url;
      console.log("url:", url);
    }

    return
  });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading