gpt4 book ai didi

javascript - Firebase 函数算法 onWrite

转载 作者:行者123 更新时间:2023-12-01 02:02:45 24 4
gpt4 key购买 nike

我希望为 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)
})
});

And my Database

我收到这样的错误

 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

关于javascript - Firebase 函数算法 onWrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50410480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com