简体   繁体   中英

Firestore query returns nothing

Here is the structure of the firestore database

ingredients->(doc name)-> ingredientName: "Apple"

I am trying to figure out the document name of the document with Apple in it but I keep running into an issue where nothing is returned.

async function getIngredientID(){
    const q = query(collection(fsdb, 'ingredients'), where('ingredientName', '==', 'Apple'));
    const querySnapshot = await getDocs(q);
    querySnapshot.forEach((doc) => {
        console.log(doc.id, " => ", doc.data());
    });
}

there is nothing that comes out on the console. At one point I console logged the value of q and there was no document there. All of the StackOverflow answers have to do with Web version 8 but I am working with Web version 9.

I imagine you must have the imports, make sure you have them all correctly. Now import >>fsdb<< make sure to start cloud firestore and get a reference to the service, check that the where method is correct as well as the collection, i don't know what information it has when initializing firebase, it would be nice if you could send more information, if this information does not help you

import { collection, getDocs, query, where } from "firebase/firestore";

import { fsdb } from '../fb';

async function getIngredientID() {
  try {
    const q = query(
      collection(fsdb, "ingredients"),
      where("ingredientName", "==", "Apple")
    );

    const { docs } = await getDocs(q);

    const data = docs.map((doc) => ({
      id: doc.id,
      ...doc.data(),
    }));
  } catch (error) {
    console.log(error);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM