gpt4 book ai didi

javascript - 使用触发器将元素添加到 firebase 实时数据库

转载 作者:行者123 更新时间:2023-11-30 19:37:33 25 4
gpt4 key购买 nike

我正在使用 firebase 云函数在我的数据库上创建字段时触发数据库更新。

exports.dbTest2 = functions.database.ref('/{uid}/ignore')
.onCreate((snapshot, context) => {

const uid = context.params.uid
console.log(`Current User ${uid}`)

// Data added at the location
const ignoreData = snapshot.val()
const endTime = new Date(ignoreData.endTime).toString()


// ref matches `/{uid}/ignore`
return snapshot.ref.parent.child('schedule').set({

allDay: false,
endDate: new Date().toString(),
id: 100,
startDate: new Date().toString(),
title: 'test'

})

});

当我将 ignore 添加到我的实时数据库时,会触发此函数。当它被触发时,它看起来像下面这样:

enter image description here

但是,我希望 ignore 是一个类似数组的结构,它具有索引,其中每个索引都包含对象。像这样:

我也试过类似return snapshot.ref.parent.child('schedule').child().set(...

但没有成功,因为 child() 需要一个参数。

有什么帮助吗?

最佳答案

您可以简单地传递一个带有对象的数组:

 return snapshot.ref.parent.child('schedule').set(
[
{
allDay: false,
endDate: new Date().toString(),
id: 100,
startDate: new Date().toString(),
title: 'test1'
},
{
allDay: false,
endDate: new Date().toString(),
id: 101,
startDate: new Date().toString(),
title: 'test2'
}
]
);

它在我的 firebase 中的样子(照片作为数组传递):firebase collection

关于javascript - 使用触发器将元素添加到 firebase 实时数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55780757/

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