1

Cloud Firestore doesn't yet support batch read of documents in a collection and so the next best thing is to use firestore.getAll() to retrieve documents given an array of ObjectId's as given in this link.

A similar question has already been posted and the above function was the answer to the question. But when I run the same snippet it somehow isn't working. Can someone please tell me why?

db.collection("users")
        .doc(uid)
        .get()
        .then(data => {
            // return array of accountId's associated with user
            return data.data().accounts;
        })
        .then(arr => {
            console.log(arr);
            if (!arr) {
                res.json({ message: "no associated accounts" });  // check if array is empty
            }
            let userAccounts = [], docRefs = []; 
            arr.forEach(id => docRefs.push(db.doc(`accounts/${id})`)));     // dynamically creating DocumentReferences for each _id
            console.log(docRefs);     // valid output -> an array of 2 DocumentReference() objects
            db.getAll(...docRefs).then(docs => {     // I
                docs.forEach(doc => {
                    if (doc.exists) {
                        console.log(doc.data());     // II
                        userAccounts.push(doc.data());
                    }
                });
            });
            console.log(userAccounts);               // III
            res.json(userAccounts);
        })
        .catch(err => {
            res.status(500).json({
                status: err.code,
                message: err.message
            });
        });
  • I. Since it's a dynamically generated array, I've used the spread operator.
  • II. This line is never executed as there is no output for this console.log() statement, not even an undefined.
  • III. Therefore the userAccounts is still [] after this, since II is never executed and neither is the push() after that and so, this is the response I get. No errors.

P.S. The arr is fine. I've cross-checked with the db. The db variable is a firestore() object.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ayush Nair
  • 43
  • 6
  • I believe the issue here is a tiny syntax error. When you call getAll(...refDocs), you have to remove the '...', that is only to specify that operator for the parameter when defining the function, not when calling it. You can see the example of usage in the [documentation](https://googleapis.dev/nodejs/firestore/latest/Firestore.html#getAll) you provided. – Pablo Almécija Rodríguez Sep 24 '19 at 08:22
  • @PabloAlmécijaRodríguez when I just use `docRefs`, I get the following message. `"message": "getAll() no longer accepts an array as its first argument. Please unpack your array and call getAll() with individual arguments."` – Ayush Nair Sep 26 '19 at 17:05
  • Indeed, I see [here](https://github.com/googleapis/nodejs-firestore/blob/a13afe2997e7f641856e340c574f610ec6b98cf0/dev/src/transaction.ts#L188) that arrays are not valid anymore. Could you then just skip the step of pushing the docs into `docsRef` and just get the document and introduce it into `userAccounts` if it exists? – Pablo Almécija Rodríguez Sep 27 '19 at 12:44

0 Answers0