首页 > 解决方案 > Javascript 中 Firebase CLI 函数中的解析错误

问题描述

我正在尝试使用 Firebase Cloud Messaging 服务和 Fireabse CLI 中的代码从 android 设备发送一对一的通知。但是由于解析错误,我无法部署代码,我无法弄清楚。

我的想法如下:出于安全原因,我理解,我需要使用 Firebase Cloud 功能来发送通知。从我的 android 代码中,我将在 Firebase 数据库中编写目标设备的令牌、消息正文和主题。一旦 write 事件发生,Firease 函数就会被部署来发送通知。这是我的 Firebase 代码

'use strict'

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

exports.sendNotification = functions.database.ref('/Notification/{receiver_token}/{message_topic}/{message_body}’).onWrite((data, context) =>
{
  const receiver_token = context.params.receiver_token;
  const message_topic = context.params.message_topic;
  const message_body = context.params.message_body;

  console.log('We have a notification to send to token: ’ , receiver_token);

  const payload = 
    {
        notification:
        {
            title: message_topic,
            body: message_body,
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(receiver_token, payload)
    .then(response => 
        {
            console.log('This was a notification feature.');
        });

});

当我尝试在 Firebase 中部署此功能时,出现以下错误:

错误解析错误:未终止的字符串常量错误:函数预部署错误:命令以非零退出代码1终止

如果需要,我可以提供完整的错误日志

标签: javascriptfirebasegoogle-cloud-functionsfirebase-cli

解决方案


这行代码看起来像一个问题:

console.log('We have a notification to send to token: ’ , receiver_token);

您的开头引号是单引号(撇号),但您的结尾引号是不同的引号。非常仔细地观察它们。使第二个匹配第一个。


推荐阅读