0

I'm brand new to NodeJS and I am trying to deploy my first firebase function. I validate the following code online and it looks good but it keeps throwing a parse error when I try to deploy it. Where is my mistake?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotificationToFCMToken = functions.firestore.document('Chats/{ChatKeyId}/messages').onWrite(async(event) -> {
    const receiverId = event.after.get('receiverId');
    const senderId = event.after.get('senderId');

    const userRef = db.collection('Users');

    const receiverDoc = await.userRef.where('userId', '==', receiverId).get;
    if(receiverDoc.empty) {
        console.log('receiverDoc empty');
        return;
    }
    receiverDoc.forEach(doc => {
        const fcmToken = doc.fcmToken;
    });

    const senderDoc = await.userRef.where('userId', '==', senderId).get;
    if (senderDoc.empty) {
        console.log('senderDoc empty')
        return;
    }
    senderDoc.forEach(doc => {
        const name = doc.name;
    });

    const content = event.after.get('content');
    let userDoc = await admin.firestore().doc('users/${uid}').get();
    let fcmToken = userDoc.get('fcm');

    var message = {
        notification: {
            title: 'New Message',
            body: 'New message from ${name}',
        },
        token: fcmToken,
    }

    let response = away admin.messaging().send(message);
    console.log(response);
});
1
  • 1
    What error? Look, if you want an answer, be detailed. "parse error" doesn't mean much. Commented Dec 4, 2020 at 18:24

1 Answer 1

1

I think you have a dot in the wrong place and you are missing parenthesis and other problems:

  • The get function of a firestore document should be get():

    // Wrong
    userRef.where('userId', '==', senderId).get;
    // Correct
    userRef.where('userId', '==', senderId).get();
    
  • Replace every occurrence of await. with await :

    // Wrong
    const receiverDoc = await.userRef.where('userId', '==', receiverId).get();
    // Correct
    const receiverDoc = await userRef.where('userId', '==', receiverId).get();
    
  • Use backticks for templating:

    let a = `users/${uid}`    // NOTE!!!!!!!! ` not ' or "
    
  • What do the forEach loops do?

Be careful and check your code again.

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful, there are other weird things in your code, but at least now it should work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.