【问题标题】:Can not deploy async Firebase function无法部署异步 Firebase 功能
【发布时间】:2021-06-25 20:56:50
【问题描述】:

我正在尝试使用以下功能添加用户角色。该功能应部署为 firebase 功能。

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

admin.initializeApp()
const db = admin.firestore()

exports.AddUserRole = functions.auth.user().onCreate(async (authUser) => {
  if (authUser.email) {
    const customClaims = {
      admin: true,
    }
    try {
      let _ = await admin.auth().setCustomUserClaims(authUser.uid, customClaims)
      return db.collection('roles').doc(authUser.uid).set({
        email: authUser.email,
        role: customClaims,
      })
    } catch (error) {
      console.log(error)
    }
  }
})

当我尝试使用 firebase deploy --only functions:AddUserRole it 进行部署时,我收到 error Parsing error: Unexpected token => 并且部署失败。

如果我删除 async/await 它工作得很好,但我需要异步等待这个和未来的功能,所以我想知道我做错了什么。

一些附加信息。我正在使用节点 v.12.21.0 和所有 firebase 工具/SDK 等的最新版本。

【问题讨论】:

  • 您确定 Firebase 支持异步/等待所需的 ECMAScript 版本吗?
  • 我看到有人在 firease 函数中使用 async/await。所以是的
  • async function (authUser) { 工作吗?
  • 已经试过了。我只是用函数得到同样的错误

标签: javascript firebase async-await firebase-authentication


【解决方案1】:

我认为您可以删除“_ = ...”并等待在角色中设置新文档的响应并返回。

这只是一个建议,但我希望它有所帮助。

exports.AddUserRole = functions.auth.user().onCreate(async (authUser) => {
  if (authUser.email) {
    const customClaims = {
      admin: true,
    }
  try {
    await admin.auth().setCustomUserClaims(authUser.uid, customClaims)
    //Or whatever you want to call it
    const updatedRole = await db.collection('roles').doc(authUser.uid).set({
      email: authUser.email,
      role: customClaims,
    })
      return updatedRole
    } catch (error) {
      console.log(error)
    }
  }
})

【讨论】:

  • 感谢您的回复,我一定会考虑到这一点,但这并不能解决我的问题。
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多