0
votes

inline editor in Dialogflow cloud function returns null for value which exists.

Cloud function from inline editor deployed without any errors. Weebhook response "Webhook execution successful"

Have tried reading other values in the same item, all returning null.

Function with aim to read temp

function handleReadTemp(agent) {
    return admin.database().ref('device/data/subdata/').limitToLast(1).once('value').then((snapshot) => {
    const value = snapshot.child('Temp').val();
     agent.add(`The temperature right now is ${value}`); 
    });
  }

df Structure of Firebase realtime database is as follows;[link] https://imgur.com/a/VLp8F9R

root
  |
  device
       |
        data
          |
          subdata
              |
              - record1
                    |- Temp:"33.35"
              -  record2
                    |- Temp: "34"

Goal is to read the the 34 from Temp of record2 - so the Temp of the last record.

My aim was that admin.database().ref('device/data/subdata/').limitToLast(1).once('value').then((snapshot) =>

gets the last record (record2 Note: record names are randomly generated so I cannot know what the next record will be called)

and then const value = snapshot.child('Temp').val();

Would get the value of Temp which is a child of record2.

Cloudfunction logs throw no errors [link] https://imgur.com/F9UUrjC

Agent response in Dialogflow: temperature is null

1

1 Answers

0
votes

This is normal because the content of the snapshot DataSnapshot corresponds to the following object:

{"record2":{"Temp":34}}

You can verify it by doing console.log(snapshot.val());

So, you should do something like:

.....
const value = snapshot
            .child(Object.keys(snapshot.val())[0] + '/Temp')
            .val();
agent.add(`The temperature right now is ${value}`);  
.....