Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove data with firebase-admin?

So, I'm using this firebase-admin library here. I read the docs from Firebase here. But I can't find the section how to remove the data with firebase-admin.

I do something like this, but it doesn't work:

router.get('/delete_category', function(req, res, next) {
  var key = req.query.item;
  let del_ref = admin.database().ref("product/" + key);
  del_ref.remove() 
});

Can you guys help me on how to delete the data from my firebase database with firebase-admin? Thanks in advance

Notes: I am using NodeJS with Express Framework and Handlebars

like image 827
yogieputra Avatar asked Feb 07 '17 18:02

yogieputra


2 Answers

Your code looks correct. The remove() method is what you want to use. You probably just need to add a completion listener to ensure that the remove() call was successful:

router.get('/delete_category', function(req, res, next) {
  var key = req.query.item;
  let del_ref = admin.database().ref("product/" + key);
  del_ref.remove()
    .then(function() {
      res.send({ status: 'ok' });
    })
    .catch(function(error) {
      console.log('Error deleting data:', error);
      res.send({ status: 'error', error: error });
    });
});
like image 166
jwngr Avatar answered Oct 03 '22 00:10

jwngr


SOLVED: Finally I found the answer
I made mistake in the references here

let del_ref = admin.database().ref("product/" + key);

It should be read the category ref like this:

let del_ref = admin.database().ref("category/" + key);
like image 26
yogieputra Avatar answered Oct 02 '22 23:10

yogieputra