Reputation: 53
I have the following code below, following the example of this link: https://gist.github.com/CodingDoug/814a75ff55d5a3f951f8a7df3979636a .Only that is returning error.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.alertHelp = functions.firestore
.document('chamados/{userId}')
.onWrite(async (change, context) => {
try {
const snapshot = change.after;
const val = snapshot.data();
const tid = val.tid;
const collection = db.collection('permissons');
const query = await collection.where('tid', '==', tid).get();
const token = query.data().fcmToken;
const promises = [];
token.forEach(fcm => {
const p = admin.firestore().doc(`{userId}/${fcm}`).get();
promises.push(p);
});
const snapshots = await Promise.all(promises);
const results = [];
snapshots.forEach(snap => {
const data = snap.data().fcmToken;
results.push(data);
})
console.log(results);
} catch (error) {
console.error(error)
}
});
Error
query.data is not a function
Upvotes: 0
Views: 211
Reputation: 317467
In this line:
const query = await collection.where('tid', '==', tid).get();
query
is going to be a QuerySnapshot type object. As you can see from the linked API documentation, it doesn't have a data()
method. That's what the error is trying to tell you.
A QuerySnapshot can contain zero or more DocumentSnapshot objects. You have to write code to check for and iterate those results. You can use the forEach
method, or the docs
property on the QuerySnapshot to do this. You might want to review the documentation about that.
Upvotes: 1