Firebase 函数 algolia onWrite

Firebase functions algolia onWrite

我想让我为firebase添加的用户自动添加到algolia中,我创建了这样一个函数,但是我得到了一些错误

我的 onWrite 函数

exports.updateIndex = functions.database.ref('/Users/{userId}').onWrite(event => {

        var client = algoliasearch(ALGOLIA_APP_ID,ALGOLIA_ADMIN_KEY);
        const index = client.initIndex('Users');

        const userId = event.params.userId;
        const data = event.data.val()

        if (!data) {
          return index.deleteObject(bookId, (err) => {
            if (err) throw err
            console.log('User Removed from Algolia Index', userId)
          })}

        data['objectID'] = userId

        return index.saveObject(data, (err, content) => {
          if (err) throw err
          console.log('User Updated in Algolia Index', data.objectID)
        })
      });

我收到这样的错误

 TypeError: Cannot read property 'userId' of undefined
        at exports.updateIndex.functions.database.ref.onWrite.event (/user_code/index.js:43:29)
        at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
        at next (native)
        at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
        at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
        at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
        at /var/tmp/worker/worker.js:716:24
        at process._tickDomainCallback (internal/process/next_tick.js:135:7)

其实我不懂database.ref('/Users/{userId}')? 我该怎么办?

改变这个:

exports.updateIndex = functions.database.ref('/Users/{userId}').onWrite(event => {
  const userId = event.params.userId;
  const data = event.data.val()

进入这个:

exports.updateIndex = functions.database.ref('/Users/{userId}').onWrite((change,context) => {
  const userId = context.params.userId;
  const data = change.after.val();

云函数已更新,onWrite 现在有两个参数 changecontext。为了能够检索通配符,您需要使用 context 参数。

The context parameter provides information about the function's execution. Identical across asynchronous functions types, context contains the fields eventId, timestamp, eventType, resource, and params.

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database