NodeJS mongodb query ERR_HTTP_HEADERS_SENT NodeJS mongodb query ERR_HTTP_HEADERS_SENT mongoose mongoose

NodeJS mongodb query ERR_HTTP_HEADERS_SENT


You are getting this error because whenever error gets occurred ,your if (err) gets called which sends response to the client and then again after if condition res.json() gets called ,which throws error as response headers were already sent in the if condition above.You can do something like this ,use else with if condition.

var page = (req.query.page) ? parseInt(req.query.page) : 0;    var qtd = (req.query.qtt) ? parseInt(req.query.qtt) : 20;    var min_value = page * qtd;    var max_value =( page + 1 ) * qtd - 1;    Order.find({}, {skip:min_value,limit:max_value}, function(err,orders) {        if (err) {            res.json({                status: "error",                message: err,            });        }    else{       res.json({            status: "success",            message: "Orders retrieved successfully",            data: orders        });}    });};


 var page = (req.query.page) ? parseInt(req.query.page) : 0;    var qtd = (req.query.qtt) ? parseInt(req.query.qtt) : 20;    var min_value = page * qtd;    var max_value =( page + 1 ) * qtd - 1;    Order.find({}, {skip:min_value,limit:max_value}, function(err,orders) {        if (err) {            res.json({                status: "error",                message: err,            });        }else{        res.json({            status: "success",            message: "Orders retrieved successfully",            data: orders        });}    });};

actually you're sending response in eirther case you were missing else


matomo