kelvin lim
kelvin lim

Reputation: 33

Firebase Query and how to read the output

enter image description here

Above is my Js code

I have a database that has information regarding the location of an apartment, I am trying to search for a specific property to see if it exists in the database. The user will be able to key into the search box to perform the search.

"propertiesRef" is used to store the user input. I tried storing the data into "q" that I received from querying the database. But I have no idea how to read the result.

enter image description here

This is the console log for "q", but I don't quite understand the information that is shown, I want to know which output in the console should I be looking at and how do I access them?

Upvotes: 0

Views: 843

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50850

The query() function just creates an instance Query. You need to use getDocs() function to actually fetch data from Firestore.

const search = (property) => {
  const propertiesRef = collection(db, "flats-table");

  const q = query(propertiesRef, where("name", "==", property))

  return getDocs(q).then((qSnap) => {
    const data = qSnap.docs.map(d => ({ id: d.id, ...d.data() }))
    console.log(data);
    return data;
  }) 

  // or use async-await
  // const qSnap = await getDocs(q);
}

Checkout the documentation for more examples.

Upvotes: 1

Related Questions