简体   繁体   中英

How to properly do something onUpdate of firebase realtime database?

Why Am I getting error in this code block?

var current_op_id = 0;

var targetRef = firebase.database().ref('operators/' + current_op_id);

    targetRef.onUpdate((change, context) => {
        //do something here
    });

Error: Uncaught TypeError: targetRef.onUpdate is not a function

Just before that code block, I have this code block that works OK.

var op_id = 0;

var updatedRef = firebase.database().ref('operators/' + op_id);

    updatedRef.on('value', function(snapshot) {
        //do something here

    });

onUpdate() is used in cloud functions, from the docs :

onUpdate() , which triggers when data is updated in the Realtime Database.

exports.dbUpdate = functions.database.ref('/path').onUpdate((change, context) => {
  const beforeData = change.before.val();
  const afterData = change.after.val(); 
});

If you want to update a field in Realtime Database, then you need to use update() . You can read more about it here:

https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM