I'm having trouble understanding how to reference and read wildcard node that has value.
RTDB (Can I have a sample path reference?):
What I've done :
// Define your RTDB Reference
const rtdbReference = admin.database().ref("Sensor MQ7");
const mq7ref = rtdbReference.child("-NHi7dBPMlVi6hXrnI03");
const valref = mq7ref.child("MQ7");
// Fetch the data
const snap = await valref.get();
const snapValue = snap.val();
// Inject snapvalue in the response
return res.status(200).send({
method: "sendMessage",
chat_id,
text: `${first_name}\n${receivedMessage}\n${snapValue}`,
});
The output : Eg
How do I make it so that it reads any wildcard node and output everything. Right now it only reads individual node when I specify it. Please help because I've been going at this since morning now its 10pm and if possible please explain like I'm 5 because I'm really really new to this.
CodePudding user response:
To read and log all nodes under Sensor MQ7
, you can do:
const reference = admin.database().ref("Sensor MQ7");
const snapshot = await reference.get();
snapshot.forEach((childSnapshot) => {
console.log(childSnapshot.child("MQ7").val());
});
In here:
snapshot
contains a snapshot of all data under theSensor MQ7
nodes.snapshot.forEach()
loops over the child nodes of the snapshot.child("MQ7")
then gets the snapshot for that specific property..val()
gets its value.