0

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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
JohnSan
  • 53
  • 6

1 Answers1

1

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I changed the code according to the documentation: [link](https://firebase.google.com/docs/firestore/query-data/queries#execute_a_query). But I am getting an deploy error. I tried to fix it with a solution here from the stackoverflow: [link](https://stackoverflow.com/questions/53354417/each-then-should-return-a-value-or-throw-firebase-cloud-functions). But it did not work. `Error: 18:21 error Each then () should return a value or throw promise / always-return 35:32 warning Avoid nesting promises promise / no-nesting` – JohnSan Oct 28 '20 at 17:58
  • If you have a new problem, post it separately. But before you do that, do a web search for that error message. It's very common. – Doug Stevenson Oct 28 '20 at 18:25
  • I already posted on another question, and it was marked as a duplicate [link](https://stackoverflow.com/questions/64565440/firestore-cloud-functions-query?noredirect=1#comment114165486_64565440). But the solution that indicated did not help me. I tried the corrections but the error remains. – JohnSan Oct 28 '20 at 18:44
  • I think you really should follow the advice in the duplicate. It's the same pattern for all JavaScript. – Doug Stevenson Oct 28 '20 at 19:37
  • I will try another way. Thank you. – JohnSan Oct 28 '20 at 20:25