【问题标题】:Updating Algolia Index Records On Firestore Update在 Firestore 更新时更新 Algolia 索引记录
【发布时间】:2019-03-21 19:55:28
【问题描述】:

我正在尝试使用 Cloud Firestore 触发器来自动更新 Algolia“用户”索引中的记录。

这是我们的云功能:

exports.updateUser = functions.firestore
.document('users/{id}').onUpdate((change, context) => {
    const afterData = change.after.data()
    return algoliaIndex.partialUpdateObjects([{afterData}]).catch((err) => {
    return console.error(new Error(err))
  })
})

在我们的 firebase 函数日志中,我们看到了这个错误:

Error: Usage: index.partialUpdateObjects(arrayOfObjects[, callback])
  at Index.partialUpdateObjects (/user_code/node_modules/algoliasearch/src/Index.js:136:11) 
  at exports.updateUser.functions.firestore.document.onUpdate (/user_code/index.js:19:26) 
  at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:114:23)
  at cloudFunction  (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:144:20)
  at /var/tmp/worker/worker.js:827:24 
  at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我们做错了什么?

谢谢!

编辑: 我已经更新了下面的代码,除了 Algolia 需要在对象中传递对象 ID 以便它更新正确的记录之外,它现在可以工作了。如何在“afterData”对象中引用 ObjectID?

【问题讨论】:

    标签: firebase vue.js google-cloud-firestore google-cloud-functions algolia


    【解决方案1】:

    partialUpdateObjects(强调对象s)期待arrayOfObjects,但您从afterData 中的Firestore 获得的数据是一个对象。

    尝试将其包装在一个数组中,或者更好的是,使用partialUpdateObject。

    https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/?language=javascript

    【讨论】:

    • 这有帮助,我越来越近了......我现在需要以某种方式包含对象 ID
    • @GregFielding [{ ...afterData, objectID: <your object id> }]
    • 感谢 imjared....firebase 不允许我在 afterData 前面部署 ...。
    • 还有别的写法吗? return algoliaIndex.partialUpdateObjects([{ ...afterData, objectID: afterData.id }])
    • let data = afterData; data.objectID = <your-object-id>; algoliaIndex.partialUpdateObjects([ afterData ]);
    【解决方案2】:

    objectId 存储在context 中,其中包含params 属性。因此,在您的 onUpdate 方法内部的某处,您应该声明如下变量:

    const objectId = context.params.*insert the key you're using*
    

    如果您不确定密钥的名称是什么,可以使用console.log(context.params),将更改部署到 Firebase 云,然后在尝试编辑文档时查看函数日志中打印的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多