javascript - 为什么我无法将此代码部署到 firebase 函数?我不明白这个错误

标签 javascript firebase firebase-realtime-database google-cloud-functions

我是 firebase 函数的新手 - 显然 - 我正在尝试测试创建时数据库的特定路径中的电子邮件是否正在被帐户使用,如果它没有被使用,则更改它相应的数据库值。代码如下:

exports.checkEmail = functions.database.ref('/checkEmailExistance')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const email = snapshot.val();
      console.log('Email:', email, context.params.pushId);
      admin.auth().getUserByEmail(email)
      .then(snapshot => {
          const data = snapshot.toJSON()

          return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
      })
});

错误是:

ERROR: /Users/nathan/Documents/FirebaseFunctionsClipify/functions/src/index.ts:41:7 - Promises must be handled appropriately
ERROR: /Users/nathan/Documents/FirebaseFunctionsClipify/functions/src/index.ts:42:13 - Shadowed name: 'snapshot'

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
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!     /Users/nathan/.npm/_logs/2019-04-25T16_21_29_696Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

更新:

我更改了代码,这样错误就不会再次产生,但仍然出现相同的错误:

exports.checkEmail = functions.database.ref('/checkEmailExistance')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const email = snapshot.val();
      console.log('Email:', email, context.params.pushId);
      return admin.auth().getUserByEmail(email)
      .then(snap => {
          const data = snap.toJSON()

          return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
      })
    });

最佳答案

第二个错误告诉您重新定义了一个名为 snapshot 的现有变量。请注意,快照是在函数回调的顶层定义的,然后又在 then 中定义。打回来。第二个实例“隐藏”第一个实例,这是代码中的潜在错误。只需将第二个变量命名为不同的名称即可。

第一个 lint 错误告诉您代码中有未处理的 Promise。您可以通过返回 admin.auth().getUserByEmail().then(...) 的 promise 来解决此问题:

  return admin.auth().getUserByEmail(email)
  .then(snap => {
      const data = snap.toJSON()

      return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
  })

关于javascript - 为什么我无法将此代码部署到 firebase 函数?我不明白这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853775/

相关文章:

javascript - 我如何在 setTimeout 中保持很长时间?

Firebase:如何调试 onCall 函数?

node.js - 允许用户仅在 firebase 上修改自己的数据

android - 火力地堡安卓 : How to read from different references sequentially

javascript - Firebase 查询从 Node 返回空对象,但不返回 API

javascript - 使用 Javascript 删除 DOM 元素

javascript - 为 WordPress 添加光滑的 slider

javascript - 如何将ckeditor html输出转换为普通字符串和限制字符串

android - Firebase 函数接收对象而不是字符串

ios - 我想在 tableView 中显示来自 firebase 的数据