Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda