首页 > 解决方案 > Javascript,意外令牌 =>

问题描述

尝试编写要部署到 firebase 的 javascript 函数时出现以下错误:

解析错误:意外的令牌 =>

在代码:

exports.sendFriendRequest = functions.firestore.document("requests/{rUid}").
onWrite(async (event) => {
    const userId1 = event.after.get("userId1");
    const userId2 = event.after.get("userId2");
    let userName1 = await admin.firestore().doc("users/${userId1}/name").get();
    console.log(userName1);

    const title = "New friend request";
    const content = "${userName1} wants to be your friend";
    console.log(content);

    let user2 = await admin.firestore().doc("users/${userId2}").get();
    let fcmToken = user2.get("fcm");

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

    var message = {
        notification: {
            title: title,
            body: content,
        },
        token: fcmToken,
    }

    let response = await admin.messaging().send(message);
    console.log(response);
});

所有的括号都匹配,我的节点版本是 14.16.1。我不知道为什么我会得到这个,有什么想法吗?我在下面上传了一张带有代码的图片。

错误图片

标签: javascriptnode.jsfirebase

解决方案


使用您当前的配置,ESLint 不了解async/await语法是什么。

您需要打开您的.eslintrc.js(或.eslintrc.*)文件,并确保它ecmaVersion至少是版本8 (ES2017)。有关更多信息,请参阅文档

// for .eslintrc.js
module.exports = {
  /* ... other settings ... */
  "parserOptions": {
    "ecmaVersion": 8,
    /* ... other settings ... */
  },
  /* ... other settings ... */
}

推荐阅读