D
D
Deodatuss2015-06-11 18:01:55
MongoDB
Deodatuss, 2015-06-11 18:01:55

Why does mongoose send a 404 response on its own?

In general, there is a method that updates the user and sends an object with updated data:

exports.update = function(req, res){
    if(req.diffUser){
        User.update({_id:req.user.id},req.diffUser,function(err){
            if(err){
                throw err;
            } else {
                User.findById(req.user.id,'-password -salt -email',function(err,user){
                   if(err){
                       throw err;
                   }
                    if(!user){
                        res.status(400).json({type:'Error',message:'User not found =/...hmm'});
                    } else {
                        res.json(user);
                    }
                });
            }
        });
    } else{
        req.status(400).json({type:'Error',message:'No difference'});
    }
};

So, for some reason not known to me, the 3rd line (User.update) sends a 404 response to the client, and then the code is executed quietly and at the end it fails with the error "Can't set headers after they are sent". Why can this happen?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-06-12
@MarcusAurelius

This is not done by the third line, but by the code that remains behind the screen. Most likely, the request routing is incorrectly built and the router does not understand that this method has taken over the return of the result. The router did not find the desired path and returned a 404.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question