0

I have created a JavaScript delete function, which removes data from a MongoDB-database. The problem is that the response is empty. Any idea why this happens and how I can solve this? I need to get case 1 working...

Response empty (1):

.delete(function(req, res) {
        var response = {};
        if (password == req.body.token) {
            mongoOp.findById(req.params.id, function(err, data) {
                if (err) {
                    response = {"error": true, "message": "Error fetching data"};
                } else {
                    mongoOp.remove({_id: req.params.id}, function(err) {
                        if (err) {
                            response = {"error": true, "message": "Error deleting data"};
                        } else {
                            response = {"error": true, "message": "Data associated with " + req.params.id + "is deleted"};
                        }                     
                    });
                } 
                res.json(response);               
            });
        }        
    })

Response not empty (2):

.delete(function(req, res) {
        var response = {};
        if (password == req.body.token) {
            mongoOp.findById(req.params.id, function(err, data) {
                if (err) {
                    response = {"error": true, "message": "Error fetching data"};
                } else {
                    mongoOp.remove({_id: req.params.id}, function(err) {
                        if (err) {
                            response = {"error": true, "message": "Error deleting data"};
                        } else {
                            response = {"error": true, "message": "Data associated with " + req.params.id + "is deleted"};
                        }
                        res.json(response);                     
                    });
                }               
            });
        }        
    })
Engo
  • 899
  • 3
  • 19
  • 49
  • @BlakesSeven I didn't know it was caused by asynchronous callbacks, therefore I am not sure if this is a duplicate question... – Engo Mar 22 '16 at 01:07
  • 1
    If you "knew" then you wouldn't have asked. The answers there are exactly the same as what you accepted. It's the most commonly asked question. It doesn't mean that because everyone does not know it isn't a duplicate. – Blakes Seven Mar 22 '16 at 01:10
  • @BlakesSeven You are right, but I was afraid that my question would be put to hold, without knowing the existence of the question you shared with me... So fortunately you shared the link of the duplicate question :) – Engo Mar 22 '16 at 01:21
  • Ah. Now *"what is a 'hold' meant to do?"* you might ask. The primary reason questions are put on "hold" is to **stop** the inevitable flood of answers ( and expecially with common duplicates, sourced by doing the search that the asker did not ) that either take guesses at an obscure question or as in this case search the web and issue the common response found. But if you also took a moment and read the marked duplicate and answers, then you would see the answer to the question. We don't need 1000's of questions and answers all saying the same thing. – Blakes Seven Mar 22 '16 at 01:29

1 Answers1

1

It's because of callbacks: res.json(response); in the first case is called before mongoOp.remove calls it's callback function(err) { ....

Node.js is asynchronous.

UPD: And it's better not to use temporary variable response and extra ifs here:

.delete(function(req, res) {
    if (password == req.body.token) {
        mongoOp.findById(req.params.id, function(err, data) {
            if (err) {
                return res.json({"error": true, "message": "Error fetching data"});
            }
                mongoOp.remove({_id: req.params.id}, function(err) {
                    if (err) {
                        return res.json({"error": true, "message": "Error deleting data"});
                    }
                        return res.json({"error": true, "message": "Data associated with " + req.params.id + "is deleted"});
                });             
        });
    }        
})
Roman Dibikhin
  • 856
  • 4
  • 15
  • It is my first day in the Node.js world. Thank you very much for your help :) I never thought it would be caused by callbacks. I have noticed that I need to read more about callbacks etc... – Engo Mar 22 '16 at 01:04
  • 1
    You're welcome! Good luck in the Node.js world. Be careful with callbacks. They can be called twice, can be undefined, can be called when you don't expect them to be called. Follow best practices like this: https://blog.risingstack.com/node-js-best-practices/ – Roman Dibikhin Mar 22 '16 at 01:14
  • Also, find some JavaScript best practices and common mistakes guides. – Roman Dibikhin Mar 22 '16 at 01:15