简体   繁体   中英

Firebase admin SDK - DELETE response not correct

I am fairy new to Javascript (but not new to programming).

I am creating a REST API using Firebase ADMIN SDK in NodeJs. I am able to perform DELETE request successfully IF the resource is present. But if it is not there (or the ID is incorrect), the response in POSTMAN is still success.

I suppose the callback function is not proper. And I may see the same problem when I implement the GET/:id and PUT/:id. Please help.

// DELETE /api/billingPlans/:id
// Delete a billing plan
routes.delete('/:id', (req, res) => {
  var ref = admin.database().ref(firebaseNode);
  //Have also tried snapshot.exists() but similar problem occurs
  return ref.child(req.params.id).once('value', function(snapshot) {
    snapshot.ref.remove(function(error){
      if(!error)
        res.status(200).json({"result" : "DELETE Success"});
      else res.status(204).json({"error" : "Not found"});
      }); 
});

I did it after pondering over it for a while. Status code 204 does not send back a properly formatted body in POSTMAN. Don't know why. Simplified the code and now the code looks like this:

// DELETE /api/billingPlans/:id
// Delete a billing plan
routes.delete('/:id', (req, res) => {
  var ref = Common.admin.database().ref(firebaseRootNode);
  ref.child(req.params.id).once('value', function(snapshot) {
      if(snapshot.exists()){
        snapshot.ref.remove();
        return res.status(200).json({"result" : "DELETE Success"});
      }
        //204 does not send back a body
        return res.status(200).json({"error" : "Not found"});
      });
});

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