0

I am faced with the problem of retrieving two data values of a single node from my firebase database and reference it in my javascript file but don't know how to go about it. I have been able to retrieve just one data value from a node (in this case "message") but I would like to add "from" as well. Most tutorials just reference one so I am really confused. So how do I get multiple data values? This is my code...

JS file

exports.sendNotification7 = functions.database.ref('/GroupChat/{Modules}/SDevtChat/{SDevtChatId}/message')
.onWrite(( change,context) =>{

// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str = "New message from System Development Group Chat: " + eventSnapshot;
console.log(eventSnapshot);

var topic = "Management.Information.System";
var payload = {
    data: {
        name: str,
        click_action: "Student_SystemsDevt"

    }
};

// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
    .then(function (response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return;
    })
    .catch(function (error) {
        console.log("Error sending message:", error);
    });
});

Database Snapshot

2 Answers 2

3

You can read from however many nodes you want in a Cloud Function. However, only one can trigger the function to run.

To read from your database use the following code:

admin.database().ref('/your/path/here').once('value').then(function(snapshot) {

var value = snapshot.val();

});

You will probably want to read from the same place that the Cloud Function was triggered. Use context.params.PARAMETER to get this information. For the example you posted your code would turn out looking something like this:

admin.database().ref('/GroupChat/'+context.params.Modules+'/SDevtChat/'+context.params.SDevtChatId+'/from').once('value').then(function(snapshot) {

var value = snapshot.val();

});
Sign up to request clarification or add additional context in comments.

Comments

2

Just trigger your function one level higher in the JSON:

exports.sendNotification7 = 
functions.database.ref('/GroupChat/{Modules}/SDevtChat/{SDevtChatId}')
.onWrite(( change,context) =>{

    // Grab the current value of what was written to the Realtime Database.
    var eventSnapshot = change.after.val();
    console.log(eventSnapshot);
    var str = "New message from System Development Group Chat: " + eventSnapshot.message;
    var from = eventSnapshot.from;

    ...

6 Comments

It worked like a charm Frank! Please but what about referencing a different node altogether?
My answer addresses reading from other places in the database
You can use the Admin SDK for that. See TerPro's answer or stackoverflow.com/questions/43913139/… and stackoverflow.com/questions/42868592/…
Hello Frank. What about the sender value? Can't seem to retrieve it.
Figured it outtttttt!! Thanks everyone!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.