how to update a realtimedatabase transaction using a cloud function?

You can use ServerValue.Increment for that:

exports.updateViewCount = functions.https.onCall((data, context) => {

    const postId = data.postId;
    const userId = data.uid;
    console.log("postId: " + postId + ", userId: " + userId);

    const postsRef = admin.database().ref('posts').child(postId);

    postsRef.child('views_count').set(firebase.database.ServerValue.increment(1))

});

You can increment it by any numerical value atomically using that structure. Here is the documentation for more details.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top