how can I write a function in proper way?

Viewed 21

this is my function code, how can I get device token properly

firebase console screenshot

can some one correct my code , error shows is in getting token

Error is as follows:

Firebase Error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array

const functions = require("firebase-functions");
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.orderCompleteTrigger =
    functions.firestore.document('Orders/{OrdersId}').onWrite((change, context) => {
    const order_id = context.params.OrdersId;

    var payload = {
        notification: {
            title: "Order Completed Successfully",
            body: "Thank you, Keep in touch",
            admin.firestore().collection('Orders').doc(`/Orders/{OrderId}/fcmToken`).get()
            // this is where error may persists
            .then((snapshots) => {
                var tokens = []; //
                if (snapshots.empty) {
                    console.log('No Devices');
                } else {
                    const tokenSnapshot = snapshots.data(); //
                    var tokens = tokenSnapshot.token; //
                }
                return admin.messaging().sendToDevice(tokens, payload).then(Response => {
                    console.log('this is the notification')
                });
            });
        });​ // This is just a sample script. Paste your real code (javascript or HTML) here.

    if ('this_is' == /an_example/) {
        of_beautifier();
    } else {
        var a = b ? (c % d) : e[f];
    }
})
1 Answers

You determine the document path with:

admin.firestore().collection('Orders').doc(`/Orders/{OrderId}/fcmToken`)

But in JavaScript template literals, the variables must be prefixed with a $, so:

admin.firestore().collection('Orders').doc(`/Orders/${OrderId}/fcmToken`)
                                                    
Related