Home > Net >  Check if value exist firebase
Check if value exist firebase

Time:04-09

I'm trying to check if there is a cart with a userID but it doesn't seem to be working enter image description here

Here is my code attempt, I don't know where I'm going wrong, I have firebase initialized

let storage = Cart.shared.firebase
    storage.child("cart").orderByChild("userId").equalTo("Eyo1DCNZU8TfB16zJDuULpKAx2V2").once("value",snapshot => {
                    if (snapshot.exists()){
                      const userData = snapshot.val();
                      console.log("exists!", userData);
                    }else{
                        console.log("nope")
                    }
                });

CodePudding user response:

You are using Firestore but writing code for Realtime database. Try the code below:

firebase.firestore().collection("cart").where("userId", "==", USER_ID).get().then((qSnap) => {
  if (qSnap.empty) {
    console.log("No cart found for user")
  } else {
    console.log("User carts")
    qSnap.docs.forEach((d) => {
      console.log(d.data())
    })
  }
}) 
  • Related