在版本 (<= v0.9.1) 之前可以像这样设置数据,现在不能了...
event.data.ref.child('thisname').set("error");
编辑:这里没有提到二传手!只有如何从 onUpdate 或 onWrite 接收值 https://firebase.google.com/docs/functions/beta-v1-diff
解决方法贴在下面
最佳答案
Firebase:(支持版本 1.0 或 2.0)
如果其他人在 onUpdate 或 onWrite 触发器内更改/设置/更新 值时遇到问题,那么这可能会有所帮助你……
首先,这是我的数据树的样子:
"users" : {
"4h23u45h23509hu346034h5943h5" : {
"address" : "Backouse 2",
"city" : "Los Angeles",
"name" : "Joseph",
...
},
"23u4g24hg234h2ui342b34hi243n" : {
"address" : "Streetouse 13",
"city" : "Los Angeles",
"name" : "Stefan",
...
现在到云函数:
之前(<= v0.9.1)
exports.updatingUser = functions.database.ref('/users/{pushId}')
.onUpdate(event => {
var address = event.data.child('address');
var city = event.data.child('city');
if (address.changed() || city.changed()) {
//generateThisname()
if (thisname == null) {
event.data.ref.child('name').set("error"); //Important Part
}
else {
event.data.ref.child('name').set(thisname); //Important Part
}
...
现在 (>= v1.0.0)
exports.updatingUser = functions.database.ref('/users/{pushId}')
.onUpdate((change, context) => {
var addressBefore = change.before.child('address').val();
var addressAfter = change.after.child('address').val();
var cityBefore = change.before.child('city').val();
var cityAfter = change.after.child('city').val();
//create reference from root to users/{pushId}/
var rootSnapshot = change.after.ref.parent.child(context.params.pushId)
if ((addressBefore !== addressAfter) || (cityBefore !== cityAfter)) {
//generateThisname()
if (thisname === null) {
rootSnapshot.child('name').set("error");
}
else {
rootSnapshot.child('name').set(thisname);
}
...
因此在设置值之前,您首先必须从数据库的根目录进行引用,然后一直到您的值并调用 set()
关于javascript - 如何更改新 Firebase onUpdate 或 onWrite 触发器中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204120/