Como consultar os filhos existentes e fazer um loop sobre um objeto para detectar se ele existe no Firebase usando uma função do Google Cloud?

Estou tentando atualizar (adicionar) alguns filhos em um banco de dados firebase. Nesse cenário, preciso atualizar um nó sem substituir todos os nós filhos pelo Google Cloud Function usando um objeto de atualização.

Esta questão deriva desta aqui:Como atualizar um nó sem substituir todos os nós filhos pelo Google Cloud Function usando um Objeto de atualização?

Minha estrutura de dados é assim:

root: { 
  doors: {
    111111111111: {
       MACaddress: "111111111111",
       inRoom: "-LBMH_8KHf_N9CvLqhzU", // I will need this value for the clone's path
       ins: {
          // I am creating several "key: pair"s here, something like:
          1525104151100: true,
          1525104151183: true,
       }
    },
    222222222222: {
       MACaddress: "222222222222",
       inRoom: "-LBMH_8KHf_N9CvLqhzU", // I will need this value for the clone's path
       ins: {
          // I am creating several "key: pair"s here, something like:
          2525104157710: true,
          2525104157711: true,
       }
    }
  },
  rooms: {
    -LBMH_8KHf_N9CvLqhzU: {
      ins: {
        // I want the function to clone the same data here:
        1525104151100: true,
        1525104151183: true,
      }
    }
  }

Estou tentando consultar os "ins" existentes e fazer um loop sobre o objeto para detectar se ele existe antes de adicioná-los, para que eu possa adicionar um sufixo à chave, caso eles já estejam lá. Esta é a minha função no momento.

 exports.updateBuildingsOuts = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
    const afterData = change.after.val(); // data after the write

    const roomPushKey = afterData.inRoom;
    const ins = afterData.ins;

    const updates = {};
    // forEach loop to update the keys individually
    Object.keys(ins).forEach(key => {
            parentPath =  ['/rooms/' + roomPushKey + '/ins/']; // defining the path where I want to check if the data exists
            // querying the "ins"
            admin.database().ref().parentPath.on('value', function(snapshot) {
                if (snapshot.hasChild(key)) {
                    updates['/rooms/' + roomPushKey + '/ins/' + key + "_a"] = true; // define 'updates' Object adding a suffix "_a"

                    return admin.database().ref().update(updates); // do the update adding the suffix "_a"

                } else {
                    updates['/rooms/' + roomPushKey + '/ins/' + key] = true; // define update Object without suffix

                    return admin.database().ref().update(updates); // do the 'updates' without suffix
                }
              });
        });   

Esta função de nuvem está recuperando um erro:

TypeError: Não é possível ler a propriedade 'on' de undefined em Object.keys.forEach.key

Tenho certeza de que sou complicado demais, mas não consegui encontrar uma lógica mais limpa para isso.

Você tem alguma sugestão de como fazer isso de maneira mais lenta? É possível?

questionAnswers(1)

yourAnswerToTheQuestion