Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Firebase Functions with Realtime DB update child of child

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:

enter image description here

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) {

enter image description here

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...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Restuctured and it works perfectly fine now:

    var db = admin.database();
        var query = db.ref('devices/' + deviceRef).orderByChild("imei").equalTo(deviceRef);
        query.on("child_added", function (snapshot) {
          snapshot.ref.update({ 
            battery: batteryRef,
            speed: speedRef,
            temperature: temperatureRef,
            lati: latitudeRef,
            longi: longitudeRef,
            updated: now})
        });

thanks to: Frank van Puffelen

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda