Skip to content
Advertisement

How to use template literal in Firebase Cloud Function condition

Is it possible to use template literal in message condition of Firebase Cloud Function?

I tried the following but it is not working.

topic1 = `${myObject.id}`
topic2 = `${myObject.name}`
var condition = "topic1 in topics || topic2 in topics";

var message = {
  notification: {
    title: 'My object',
    body: 'My object.'
  },
  condition: condition
};

admin.messaging().send(message);

I changed to this but it is still not working:

      topic = "_full";
      topic1 = `${myObj.field1}` + topic;
      topic2 = (`${myObj.field2.field1}_${myObj.field2.field2}` + topic)
        .toString()
        .toLowerCase()
        .split(" ")
        .join("_");
      topic3 = `${myObj.field3.field1}` + topic;
      topic4 = `${myObj.field4.field1}` + topic;

      condition = `${topic1} in topics || ${topic2} in topics || ${topic3} in topics || ${topic4} in topics`;

I get “Error: Invalid condition expression provided.”

Advertisement

Answer

Don’t forget to add single quotes around topics:

var condition = `'${topic1}' in topics || '${topic2}' in topics`;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement