Lance Samaria
Lance Samaria

Reputation: 19572

Firebase Cloud Function updating ref with incorrect values

I want to add a new node to the database if the node doesn't exist. I don't want to return anything to the client, I just want to update the database with the new values. On the client I have a listener that observes the credit_counts property, once the update happens it receives it there and notifies all users that this particular user has a new credit.

In the code below I check to see if (!snapshot.exists() and if it's not there I add the node to the database using admin.database().ref('/user_credits/{creditId}/{userId}').set({ dict });. After pasting the url I check the db and the layout is:

enter image description here

I'm a Swift developer. In Swift I can just do:

Database.database().reference().child("/user_credits/\(creditId)/\(userId)").setValue(dict) and the tree will be correct.

user_credits > {creditId} > {userId} > dict are incorrect. It should be user_credits > sample_123 > user_xyz > dict values. Where am I going wrong at?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.updateViewsCtAtPostsRef = functions.https.onRequest((request, response) => {

    const currentTimeStamp = Date.now();
    const receivedTimeStamp = admin.database.ServerValue.TIMESTAMP;

    const creditId = "sample_123";
    const userId = "userId_xyz";

    admin.database().ref('user_credits').child(creditId).child(userId).once('value', snapshot => {

        if (!snapshot.exists()) {

            var dict = {
                "joined_date": receivedTimeStamp,
                "timeStamp": receivedTimeStamp,
                "credits_count": 1
            };

            return admin.database().ref('/user_credits/{creditId}/{userId}').set({ dict });

        } else {

            const previousTimeStamp = snapshot.child("timeStamp").val();
            const creditsCount = snapshot.child("credits_count").val();

            if (previousTimeStamp + whatever) < currentTimeStamp {

                let updatedCount = creditsCount + 1

                return admin.database().ref('/user_credits/{creditId}/{userId}').update({ "timeStamp": receivedTimeStamp, "credits_count": updatedCount });

            } else {

               return true
            }
        }
    });
});

Upvotes: 0

Views: 86

Answers (2)

Lance Samaria
Lance Samaria

Reputation: 19572

I had to change the ref to:

return admin.database().ref('/user_credits/' + creditId + '/' + userId).set({ "joined_date": receivedTimeStamp, "timeStamp": receivedTimeStamp, "credits_count": 1 });

I also had to update the ref inside the else statement to follow the same format.

Upvotes: 1

Martin Zeitler
Martin Zeitler

Reputation: 76609

The syntax is fine, but the reference does not match the structure; that should rather be:

admin.database().ref('user_credits').child(creditId).child(userId).child('dict')

... else there won't be any snapshot.child("timeStamp") or snapshot.child("credits_count").

Upvotes: 0

Related Questions