Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore getAll() method doesnt accept FirebaseFirestore.DocumentReference[] array

I am trying to use getAll() method in a firestore cloud function. The problem is when I use an array in getAll(), it throws an error:

[ts] Argument of type 'DocumentReference[]' is not assignable to parameter of type 'DocumentReference'. Property 'id' is missing in type 'DocumentReference[]'. const questionRefs: FirebaseFirestore.DocumentReference[]

getAll() method looks like, it accepts FirebaseFirestore.DocumentReference[] array, but it doesn't. Any idea, where the problem is?

const questionRefs = new Array<FirebaseFirestore.DocumentReference>();
for (const questionID of questions) {
    const ref = admin.firestore().collection('question-bank').doc(questionID)
    questionRefs.push(ref);
}
// then use getAll
admin.firestore().getAll(questionRefs).then(snapshots=>{
    snapshots.forEach(snapshot=>{
        // snapshot.data
        console.log('A');
    })
}).catch(err=>console.log('B'));
like image 742
meh met Avatar asked Apr 11 '18 13:04

meh met


People also ask

How do I fetch data from firestore in react native?

Step 1: Create a new React application. We use create-react-app to create our application. Step 2: Install the firebase package in the project using npm. Step 3: Create a new project from the Firebase dashboard by filling in the necessary details and check the format of the data that is stored in Firestore.

Which function is used to fetch data from the firebase document?

The Get() function in Go unmarshals the data into a given data structure. Notice that we used the value event type in the example above, which reads the entire contents of a Firebase database reference, even if only one piece of data changed.


1 Answers

According to the API docs, the signature for getAll() looks like this:

getAll(...documents) returns Promise containing Array of DocumentSnapshot

That ...documents syntax in the argument is saying that you can pass a bunch of DocumentReference objects separately (as shown in the sample code in the doc). But if you want to pass an array, you have to use the spread operator to convert the array in to individual arguments:

admin.firestore().getAll(...questionRefs)
like image 142
Doug Stevenson Avatar answered Nov 09 '22 05:11

Doug Stevenson