我正在尝试使用云火焰库触发器来自动更新我们的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?
partialUpdateObjects
(强调对象)期待arrayOfObjects
,但你从afterData
的Firestore获得的数据是一个对象。
尝试将其包装在一个数组或更好,但使用partialUpdateObject
。
https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/?language=javascript
objectId存储在context
中,其中包含params
属性。所以在你的onUpdate
方法的某个地方,你应该声明变量,如下所示:
const objectId = context.params.*insert the key you're using*
如果您不确定密钥的名称是什么,可以使用console.log(context.params)
,将更改部署到Firebase云,然后在尝试编辑文档时查看功能日志中打印的内容。