Google cloud firestore 当另一个字段发生更改时,如何使用google cloud功能更新firestore数据库中的字段?
我想为firestore数据库编写一个简单的google云函数,当同一文档中的另一个字段发生更改时,该函数会更新文档中的一个字段。触发的字段名为“copper”,将对字段“coppervalue”进行更新。我为此编写了一个简单的函数,它不会给出任何错误,但也不会更新字段“coppervalue”,所以我想知道我哪里做错了 以下是我的云功能代码:Google cloud firestore 当另一个字段发生更改时,如何使用google cloud功能更新firestore数据库中的字段?,google-cloud-firestore,google-cloud-functions,Google Cloud Firestore,Google Cloud Functions,我想为firestore数据库编写一个简单的google云函数,当同一文档中的另一个字段发生更改时,该函数会更新文档中的一个字段。触发的字段名为“copper”,将对字段“coppervalue”进行更新。我为此编写了一个简单的函数,它不会给出任何错误,但也不会更新字段“coppervalue”,所以我想知道我哪里做错了 以下是我的云功能代码: const functions = require('firebase-functions'); exports.copperupdate = fun
const functions = require('firebase-functions');
exports.copperupdate = functions.firestore
.document("/kullanici/{uid}")
.onUpdate((change,context) => {
const newfieldvalue = change.after.data();
const fieldname = newfieldvalue.name;
if(fieldname==="copper"){
const d = new Date();
const currenttime = d.getTime();
const coppervalue = snap.data()['coppervalue'];
const copperdate = snap.data()['copperdate'];
const copperdec = (currenttime-copperdate)/1000
const copper_real= (copperdec*copper/60)+coppervalue;
const sonuccopper = Math.trunc(copper_real)
return change.after.ref.set({
coppervalue: sonuccopper
}, {merge: true});
}else{
return false;
}
});
提前谢谢。终于明白了 我的错误是我认为newfieldvalue.name是访问字段的名称,而不是访问“name”字段,所以我做了一些更改,下面是代码
const functions = require('firebase-functions');
exports.copperupdate = functions.firestore
.document("/kullanici/{uid}")
.onUpdate((change,context) => {
const newfieldvalue = change.after.data();
const previousfieldvalue = change.before.data();
if (newfieldvalue.copper === previousfieldvalue.copper){
return false;
}else{
const d = new Date();
const currenttime = d.getTime();
const copper = newfieldvalue.copper;
const coppervalue = newfieldvalue.coppervalue;
const copperdate = newfieldvalue.copperdate;
const copperdec = (currenttime-copperdate)/1000
const copper_real= (copperdec*copper/60)+coppervalue;
const sonuccopper = Math.trunc(copper_real)
return change.after.ref.set({
coppervalue: sonuccopper
}, {merge: true});
}
});