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'));
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With