Javascript Google云函数OnWrite如何只获取新添加的子项
我试图只获取数据库中新添加的子项,因此我尝试从snapshot.before中减去snapshot.after。不幸的是,这个不起作用。我的代码如下:Javascript Google云函数OnWrite如何只获取新添加的子项,javascript,google-cloud-functions,Javascript,Google Cloud Functions,我试图只获取数据库中新添加的子项,因此我尝试从snapshot.before中减去snapshot.after。不幸的是,这个不起作用。我的代码如下: const functions = require('firebase-functions'); // Create and Deploy Your First Cloud Functions // https://firebase.google.com/docs/functions/write-firebase-function
const functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
// exports.helloWorld = functions.https.onRequest((request, response) =>
//{
// response.send("Hello from Firebase!");
// })
exports.gameLoopBeing = functions.database.ref('/Chat/{pushId}')
.onWrite((snapshot, context) => {
//I want to retrieve the pushID
const original = snapshot.before.val();
const newValue = snapshot.after.val();
const difference = newValue-original
console.log('alool',context.params.pushId, difference);
// const uppercase = original.toUpperCase();
return snapshot.ref.set(original);
});
如果你想从函数中获取新添加的数据,你需要这样做
exports.gameLoopBeing = functions.database.ref('/Chat/{pushId}').onWrite((snapshot, context) => {
if (snapshot.after.exists() && !snapshot.before.exists()) {
const new_data = snapshot.after.val();
} else {
return null;
}
});