Firebase-云功能-“错误:需要使用事件参数调用云功能。”

时间:2018-10-10 23:35:31

标签: firebase firebase-authentication google-cloud-functions

我正在尝试创建一个云功能,用于在创建新用户时创建用户个人资料(很多“创建”。)

我实现了此功能:

exports.createProfile = functions.auth.user()
  .onCreate( (userRecord, context) => {
  return admin.database().ref(`/userProfile/${userRecord.data.uid}`).set({
    email: userRecord.data.email
  });
});

但是当我创建一个新用户时,出现此错误:

Error: Cloud function needs to be called with an event parameter.If you are writing unit tests, please use the Node module firebase-functions-fake.
    at Object.<anonymous> (/srv/node_modules/firebase-functions/lib/cloud-functions.js:84:19)
    at Generator.next (<anonymous>)
    at /srv/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at new Promise (<anonymous>)
    at __awaiter (/srv/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /worker/worker.js:731:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

有人面对这样的事情吗? 到目前为止有什么解决办法吗?

亲切问候

2 个答案:

答案 0 :(得分:0)

根据文档,onCreate仅将一个事件作为参数,并且不需要上下文(在这种情况下为userRecord):https://firebase.google.com/docs/functions/auth-events

然后,您应该能够通过userRecord.uid来简单地访问uid:)

答案 1 :(得分:0)

我已升级到最新的firebase功能,从而解决了系统上的问题。运行

npm outdated

查看您的firebase功能是否真的是最新的。 NPM本身不会更新过去的版本。

我的猜测是firebase-functions定义了一个事件类,正是它们的内部将这个事件对象传递给用户定义的回调。因此,如果您使用的是较旧版本的firebase-functions,则回调未收到“事件参数”

对炼金术士Shahed的回答没有足够的观点发表评论,但是文档实际上在如何调用onCreate()上似乎有点矛盾。

https://firebase.google.com/docs/functions/auth-events#trigger_a_function_on_user_creation 说:

exports.sendWelcomeEmail = 
  functions.auth.user().onCreate((user) => {
  // ...
});

但是升级指南 https://firebase.google.com/docs/functions/beta-v1-diff#authentication 表演

exports.authAction = 
  functions.auth.user().onCreate((userRecord, context) => {
    const creationTime = userRecord.metadata.creationTime; // 
       // 2016-12-15T19:37:37.059Z
    const lastSignInTime = userRecord.metadata.lastSignInTime; 
       // 2018-01-03T16:23:12.051Z
}

https://firebase.google.com/docs/reference/functions/providers_auth_.userbuilder#on-create

还显示了(user: UserRecord, context: EventContext): PromiseLike<any> | any处理程序

因此不清楚是否需要context参数。