我想访问更改后的数据。快照后(来自 Firebase 实时数据库) 这是代码:
exports.requestListener = onValueUpdated('/ride_info/{pushId}', async (change,context) => {
//const data = snapshot.val; // Use snapshot.val() to get the data
// Access `before` and `after` snapshots
// Log the change object to debug the structure
console.log('Change Object:', JSON.stringify(change, null, 2));
const beforeSnapshot = change.before;
const afterSnapshot = change.after;
const before = change.before.val(); // Get the data before the update
const after = change.after.val();
const id_customer = after.customerId;
console.log(id_customer);
const id_driver = after.driverId;
const id_ride = context.params.pushId;
这是变更对象的结构:
更改对象:
{
"source": "//firebasedatabase.googleapis.com/projects/_/locations/europe-west1/instances/*********",
"time": "2024-05-24T06:49:57.561Z",
"id": "*******",
"ref": "ride_info/NydMFHRB89yV1ODcc1z",
"instance": "********",
"specversion": "1.0",
"subject": "refs/ride_info/NydMFHRB89yV1ODcc1z",
"location": "europe-west1",
"type": "google.firebase.database.ref.v1.updated",
"data": {
"before": {
"SelectMinutes": 0,
"SelectedHour": 0,
"calculated_distance": 0,
"calculated_duration": 0,
"creation_timestamp": 1716533396799,
"customerId": "79axhZrQzVcIbl3I6J2i6PJ40oQ2",
"ended": false,
"pickup": {
"lat": 48.8578763,
"lng": 2.4061916,
"name": "Paris, Square de la Salamandre"
},
"price_calculated": false,
"rating": -1,
"rating_calculated": false,
"service": "type_1",
"state": 0,
"validated": false
},
"after": {
"SelectMinutes": 0,
"SelectedHour": 0,
"calculated_distance": 0.008233495056629181,
"calculated_duration": 0,
"creation_timestamp": 1716533396799,
"customerId": "79axhZrQzVcIbl3I6J2i6PJ40oQ2",
"ended": false,
"pickup": {
"lat": 48.8578763,
"lng": 2.4061916,
"name": "Paris, Square de la Salamandre"
},
"price_calculated": false,
"rating": -1,
"rating_calculated": false,
"service": "type_1",
"state": 0,
"validated": false,
"calculated_Price": 36,
"destination": {
"lng": 2.4061738,
"lat": 48.8578032
}
}
},
"traceparent": "****************",
"firebaseDatabaseHost": "europe-west1.firebasedatabase.app",
"params": {
"pushId": "NydMFHRB89yV1ODcc1z"
}
}
我无法访问快照后的值:我得到了
TypeError:无法在此行读取未定义的属性(读取“val”) const after = change.after.val();
您在代码中混合了第一代和第二代 Cloud Functions 的语法,这是行不通的。在第二代 Cloud Functions 中,您可以使用以下方式读取之前的值:
exports makeUppercase = onValueWritten("/messages/{pushId}/original", (event) => {
// Only edit data when it is first created.
if (event.data.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!event.data.after.exists()) {
return null;
}
// Grab the current value of what was written to the Realtime Database.
const original = event.data.after.val();
console.log('Uppercasing', event.params.pushId, original);
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 event.data.after.ref.parent.child('uppercase').set(uppercase);
});