Home > Blockchain >  Firebase Functions Read from Firestore Database fails without an error
Firebase Functions Read from Firestore Database fails without an error

Time:02-05

I'm trying to read data from my Firestore database in my cloud functions.

My Database:

cases -> 1 -> (number) min: 1

My code in my function https.onCall( async (data, context):

if(!data.caseslot) {
        console.log("no caseslot");
        //throw exception
    }
    else{
        console.log(data.caseslot); //this logs me 1
        var db = admin.firestore();
        db.collection('cases').doc('${data.caseslot}').get().then(snapshot => {
            console.log(snapshot.data().min); //"TypeError: Cannot read properties of undefined (reading 'min')"
        }).catch(reason => {
            console.log(reason);
        });
    }

At this point, I don't know what I'm doing wrong. I double checked my database...

CodePudding user response:

You have enclosed ${data.caseslot} in single quotes, you have to enclose that in back ticks

if (!data.caseslot) {
  console.log("no caseslot");
  //throw exception
} else {
  console.log(data.caseslot);
  var db = admin.firestore();
  db.collection("cases")
    .doc(`${data.caseslot}`) //enclose in back ticks (``)
    .get()
    .then((snapshot) => {
      console.log(snapshot.data().min);
    })
    .catch((reason) => {
      console.log(reason);
    });
}
  •  Tags:  
  • Related