Javascript 11:1错误分析错误:意外标记}

Javascript 11:1错误分析错误:意外标记},javascript,node.js,npm,Javascript,Node.js,Npm,我不知道这里怎么了。我看过其他帖子,但没有得到答案。错误是: 11:1 error Parsing error: Unexpected token } ✖ 1 problem (1 error, 0 warnings) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! functions@ lint: `eslint .` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the fu

我不知道这里怎么了。我看过其他帖子,但没有得到答案。错误是:

11:1  error  Parsing error: Unexpected token }

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely     additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\HP\AppData\Roaming\npm-cache\_logs\2018-10-01T18_46_12_434Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1
这是我的代码:

the index.js file is
use-strict'
const functions = require('firebase-functions');
const admin= require('firebase-admin');
admin.initializeApp(functions.config().firestore);
exports.sendNotification= functions.firestore.document('Users/{user_id}/Notifications/{notification_id}').onWrite(event=> {});

const user_id= event.params.user_id;
const notification_id= event.params.notification_id;

console.log("User ID: " + user_id +"| Notifications ID : " + notification_id);
});

尝试删除孤立的(代码>)在第11行

我不确定您是否试图在第5行中使用,但如果是,请确保在字符串周围使用引号(`),在这一行的括号之前使用($):

exports.sendNotification= functions.firestore.document('Users/{user_id}/Notifications/{notification_id}').onWrite(event=> {});
它应该是这样的:

exports.sendNotification= functions.firestore.document(`Users/${user_id}/Notifications/${notification_id}`).onWrite(event=> {});

我相信你错过了一个:

在“严格使用”声明的开头

•随机
})
在代码末尾。删除
})

•使用
${variable here}

请尝试以下操作:

"use-strict"

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

admin.initializeApp(functions.config().firestore);
exports.sendNotification = functions.firestore.document(`Users/${user_id}/Notifications/${notification_id}`).onWrite(event => {});

const user_id = event.params.user_id;
const notification_id = event.params.notification_id;

console.log("User ID: " + user_id + "| Notifications ID : " + notification_id);

您提供的代码片段有一些明显的语法错误,正如其他答案所指出的(还有一个额外的
})在脚本的末尾,您需要确保
use strict
语句的两端都有引号。
index.js文件位于代码段顶部的
行看起来像是您在创建帖子时出错了,因此您应该将这一行从代码段中移出,或者如果它在实际脚本中,请将其删除或在其前面加上
/
以注释掉

您使用的是哪个版本的NodeJS?如果您使用的是旧版本的node,则可能使用的箭头函数语法与您的node版本不兼容

  • 对照列表检查您的节点版本
    node-v
  • 如果您的节点版本表明它不支持箭头函数,请尝试将以下内容替换为:
    event=>{}
    function(event){}

Edit:您提供给
onWrite
的箭头函数似乎与
onWrite
方法中定义的参数不匹配。看起来arrow函数应该采用以下参数
(更改,上下文)=>{/*方法体*/}

exports.modifyUser = functions.firestore
  .document('users/{userID}')
  .onWrite((change, context) => {
    // Get an object with the current document value.
    // If the document does not exist, it has been deleted.
    const document = change.after.exists ? change.after.data() : null;

    // Get an object with the previous document value (update/delete)
    const oldDocument = change.before.data();

    // perform desired operations ...
  });

编辑2:您看到未定义的
user\u id
出现错误,因为您不应该将
{user\u id}
的文字字符串文本传递给Firestore。这只是一个占位符,表示您需要提供的值。您应该提供一个有效的用户id。例如,如果您的用户id为
joe
,通知id为
1
,则应按如下方式传递值:
functions.firestore.document('Users/joe/Notifications/1')

在对
console.log
的调用中,您还引用了以下变量,这些变量似乎也未定义:
console.log(“用户ID:+User\u ID+”;通知ID:+notification\u ID”)。您是否在代码的其他地方设置了这些?我看不到它们的位置。如果没有,您需要在引用它们之前将它们设置在某个位置。一旦设置了这些变量,您就需要更新对Firestore方法的调用,如下所示(请注意使用回勾“`”,而不是引号)。这称为字符串模板。类似这样的情况(显然使用
user\u id
notification\u id
的实际值):


模板文字看起来也不正确。它应该是
`Users/${user\u id}/Notifications/${notification\u id}
您还需要在代码中的某个地方定义
user\u id
notification\u id
变量(除非它们是全局可用的或其他变量)如果您确实试图在此处使用字符串模板。似乎不清楚您是否正在尝试为
'Users/{user\u id}/Notifications/{notification\u id}'
-是否确定OP在此处尝试使用字符串模板表达式?现在删除});它给出了以下错误:解析函数触发器时出错。ReferenceError:对象上未定义事件。(C:\Users\HP\Desktop\firebasepushNotifications\functions\index.js:7:16)at Module.(internal/modules/cjs/loader.js:689:30)at Object.Module.(internal/modules/cjs/loader.js:689:30)(extensions..js/modules/cjs/cjs/loader.js:700:10)at Module.load(internal/modules/C在Function.Module.\u load(internal/modules/cjs/loader.js:530:3)中,我更新了我的答案。看起来您提供给
onWrite
方法的箭头函数不正确。现在我面临以下错误:TypeError:无法在cloudFunctionNewSignature读取未定义的exports.sendNotification.functions.firestore.document.onWrite.event(/user\u code/index.js:7:28)的属性“user\u id”(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:105:23)在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:135:20)在/var/tmp/worker/worker.js:733:24在process.\u tickDomainCallback(internal/process/next_tick.js:135:7)成功部署后,在firebase函数日志中。
var user_id = "joe";
var notification_id = 1;
functions.firestore.document(
    `Users/${user_id}/Notifications/${notification_id}`)