如何在 Firebase Cloud Function 条件中使用模板文字

How to use template literal in Firebase Cloud Function condition

是否可以在 Firebase Cloud Function 的消息条件中使用模板文字?

我尝试了以下但它不起作用。

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);

我改成了这个,但还是不行:

      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`;

我收到“错误:提供的条件表达式无效。”

因为你有 "topic1 in topics || topic2 in topics" 它是一个普通的字符串,其中的变量没有展开。 topic1 也不是变量,而是 ${topic1}.

您可以执行以下任一操作:

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

或者:

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

不要忘记在主题周围添加单引号:

var condition = `'${topic1}' in topics || '${topic2}' in topics`;