I have a child that get created inside my RTDB in firebase. This happens by a webservice that sends this data to firebase via the Rest API. It happens every 10 min. The data arrives and the function picks it up when it occurs. The node hold a couple of values but specifically a device: "3523EB" value that's needed for the next part. Below is the structure of that node data:
The 'device' value here "3523EB" need to update a different table that it needs to go look for with and equalTo in the devices table. Problem is the devices table has the userID and the key. So its 2 un-known values that it needs to search through as the chils its looking for contains imei:"3523EB" and it's only that child that needs to be updated.
I've tried using ref.orderByChild("imei").equalTo(deviceRef).on("child_added", function(snapshot)
{
So device:"3523EB" in sigfox table has to look for and update only the child with this in is data as imei:"3523EB" in the devices table, but the {UserId} and the {key} where the table resides in devices is unknow.
Getting the values from sigfox in the function as it hits the DB is no issue: to take the value and look for the child in devices then update that child proves to be a challenge.
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 help...