最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the document ref to a where clause firestore? - Stack Overflow

programmeradmin1浏览0评论

How can I get a reference to the results of a where clause? My database is structured like this:

- Restaurants
     - Restaurant 1
         - Ratings (sub collection)
         - Statistics

So I have a restaurants collection where each doc is specific restaurant. And in each doc, there is a subcollection called ratings. I'm trying to get a document reference to restaurant 1 so I can add some general statistics to it, and also a reference to the subcollection so I can add a rating. I'm currently stuck on getting a reference to the restaurant 1 because I use a where clause, which doesn't return a reference.

var restaurantRef = firestore.collection("restaurants").where("name", "==", "Neubig Hall")

  function addFeedback(data) {
    return firestore.runTransaction((transaction) => {
      var feedbackRef = restaurantRef.get().then(snapshot => {snapshot.forEach(doc => doc.ref.collection("feedback"))});

It's saying restaurantRef is not a document reference. I'm using this in a React Native app.

How can I get a reference to the results of a where clause? My database is structured like this:

- Restaurants
     - Restaurant 1
         - Ratings (sub collection)
         - Statistics

So I have a restaurants collection where each doc is specific restaurant. And in each doc, there is a subcollection called ratings. I'm trying to get a document reference to restaurant 1 so I can add some general statistics to it, and also a reference to the subcollection so I can add a rating. I'm currently stuck on getting a reference to the restaurant 1 because I use a where clause, which doesn't return a reference.

var restaurantRef = firestore.collection("restaurants").where("name", "==", "Neubig Hall")

  function addFeedback(data) {
    return firestore.runTransaction((transaction) => {
      var feedbackRef = restaurantRef.get().then(snapshot => {snapshot.forEach(doc => doc.ref.collection("feedback"))});

It's saying restaurantRef is not a document reference. I'm using this in a React Native app.

Share Improve this question edited Dec 13, 2023 at 8:34 Renaud Tarnec 83.3k10 gold badges98 silver badges129 bronze badges asked Jul 21, 2020 at 4:59 aspiringsomeoneaspiringsomeone 6234 gold badges11 silver badges20 bronze badges 3
  • Please edit the question to show the code that's not working the way you expect. Please be clear where you are stuck in writing that code. – Doug Stevenson Commented Jul 21, 2020 at 5:12
  • which language you are programming in? It's different for every language. – Harshit Ruwali Commented Jul 21, 2020 at 5:21
  • Just added my code and I'm using java-script for react native – aspiringsomeone Commented Jul 21, 2020 at 13:16
Add a ment  | 

2 Answers 2

Reset to default 4

As you can see from the API documentation, where() returns a Query object. It's not a DocumentReference.

Even if you think that a query will only return one document, you still have to write code to deal with the fact that it could return zero or more documents in a QuerySnapshot object. I suggest reviewing the documentation on queries to see examples.

Also note that you can't use a Query object in a transaction. Transactions require a DocumentReference, which again, you don't have here.

If you do want to execute the query and work with the documents it returns, it will go more like this:

const restaurantQuery = firestore.collection("restaurants").where("name", "==", "Neubig Hall")
restaurantQuery.get().then(querySnapshot => {
    if (!querySnapshot.empty) {
        const snapshot = querySnapshot.docs[0]  // use only the first document, but there could be more
        const documentRef = snapshot.ref  // now you have a DocumentReference
        //  ... use your DocumentReference ...
    }
    else {
        // decide what you want to do if the query returns no documents.
    }
})

You have a typo in your code: you call restauranteRef.get() with an e at the end of restaurant, while your query is declared as restaurantRef, without e.

You should do:

  function addFeedback(data) {
    return firestore.runTransaction((transaction) => {
      var feedbackRef = restaurantRef.get()then(...)   // <- without e
      // ...
     }
发布评论

评论列表(0)

  1. 暂无评论