我写了这个方法,它应该console.log触发的节点数据,但我得到了错误。
这就是我尝试过的">
exports.makeUppercase = functions.database
.ref('/users/{userId}/matches')
.onWrite((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
//const original = snapshot.val();
console.log('OnWrite works' + snapshot.after.val());
// const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return null;
});
这是错误:makeUppercaseTypeError:snapshot.val不是exports.makeUppercase.functions.database.ref.onWrite(/srv/index.js:49:44(at cloudFunction(/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23(at/worker/worker.js:825:24 at process的函数_tickDomainCallback(internal/process/next_tick.js:229:7(
我做错什么了吗?
来自文档:
事件数据现在是DataSnapshot。
在早期版本中,event.data是DeltaSnapshot;从1.0版开始,它就是一个DataSnapshot。
对于onWrite和onUpdate事件,数据参数具有before和after字段。每一个都是一个DataSnapshot,具有admin.database.DataSnapshot.中可用的相同方法
例如:
exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
const beforeData = change.before.val(); // data before the write
const afterData = change.after.val(); // data after the write
});
因此,在您的代码中,您需要使用after
属性来检索写入后的,或者使用before
属性:
const original = snapshot.after.val();