I have an app (react-native) I am making a search query that finds a specific document based on one of the documents fields, it is a barcode that can be scanned, and then using the scanned value want to look for the object in the firebase, every object has the field that holds the value for the barcode. I just want to find it.
I followed this tutorial.
this is what I have currently:
useEffect(() => {
saveData();
}, []);
const saveData = async () => {
const productRef = db.collection("products");
const queryRef = await productRef.where("barCode", "==", data).get();
console.log(queryRef);
}
but this doesn't return the correct result, what am I missing?
this is the screenshot of console log but at the end I have this:
(truncated to the first 10000 characters)
const queryRef = await productRef.where("barCode", "==", data).get();
This query returns a QuerySnapshot which has a docs property that is an array of DocumentSnapshot. You can them map a new array containing the data from documents and set it in state:
const [products, setProducts] = useState([])
const saveData = async () => {
const productRef = db.collection("products");
const queryRef = await productRef.where("barCode", "==", data).get();
setProducts(queryRef.doc.map(d => ({id: d.id, ...d.data()})));
}