Every 10 minutes, a child is created inside my Realtime Database in Firebase through a webservice that sends data via the Rest API. When this data arrives, a function picks it up. The node contains various values, but the important one for further processing is the device identifier "3523EB". Below is the structure of the node's data:
https://i.sstatic.net/Bpy70.png
The 'device' value "3523EB" needs to be used to update a different table by searching for a match in the devices table based on the imei:"3523EB" value present in the child that requires updating. However, the devices table has unknown values for userID and key, making it challenging to locate the specific child.
I attempted to use
ref.orderByChild("imei").equalTo(deviceRef).on("child_added", function(snapshot)
{
https://i.sstatic.net/igc3m.png
The goal is for the device: "3523EB" entry in the sigfox table to locate and update only the child with imei:"3523EB" in the devices table, where the exact {UserId} and {key} are unknown.
Retrieving values from sigfox when accessing the database is not a problem; however, identifying and updating the corresponding child in devices proves to be difficult.
exports.UpdateDeviceData = functions.database.ref('sigfox/{key}/')
.onCreate((snapshot, context) => {
const deviceRef = snapshot.val()['device'];
const batteryRef = snapshot.val()['battery'];
const speedRef = snapshot.val()['speed'];
const temperatureRef = snapshot.val()['temperature'];
const latitudeRef = snapshot.val()['latitude'];
const longitudeRef = snapshot.val()['longitude'];
const timeRef = snapshot.val()['time'];
const now = new Date().getTime();
functions.logger.log('Sigfox Data Updated for device: ', deviceRef);
var db = admin.database();
var refi = db.ref("devices/");
refi.once("value", function(snapshot) {
snapshot.forEach(function(child) {
var key = child.key;
snapshot.ref.update({
battery: batteryRef,
speed: speedRef,
temperature: temperatureRef,
lati: latitudeRef,
longi: longitudeRef,
updateTime: timeRef })
functions.logger.log('Device Key Details: ',child.val());
})
});
return null;
});
Please assist...