Home > other >  FireStore Query Return Nothing
FireStore Query Return Nothing

Time:09-05

I have my user authenticated and would like them to poll on some options. Each authenticated user would be registered in a document in a collection. This is a serverless frontend react app.

const handleSubmit = async () => {
  try {
    await setDoc(doc(db, "data", user), {
      user: user,
      behave: choice === "behave" ? 1 : 0,
      grimace: choice === "grimace" ? 1 : 0,
    });
  } catch (e) {
    console.log(e.message);
  }
  try {
    const q = query(collection(db, "data"), where("uid", "==", user));
    const snapShot = await getDocs(q);
    snapShot.forEach((doc) =>
      console.log(doc)
    )
  } catch (e) {
    console.log(e.message);
  }
}

My idea is i would like to avoid duplicate poll by reference to the uid of the user. When querying data and I didn't see any error message nor I didn't see any console.log(doc), please kindly advise me what I have done wrong.

CodePudding user response:

You are adding the User ID as user in the document but then querying on field uid. Either set the field name to uid or change your query to user field as shown below:

await setDoc(doc(db, "data", user), {
  uid: user, // <-- set field name to UID
  // ...
});


// query on "uid" field
const q = query(collection(db, "data"), where("uid", "==", user));
  • Related