简体   繁体   中英

Can't Set Headers After they are sent - NodeJS

I have a node js app and one of the routes I keep getting "Can't set headers after they are sent error".

What the route does:

Users in my app have certain access levels so this route goes through the users accessLevel array and finds the appropriate access level for this route. And based on the access level of the user who's calling the route has it performs different actions.

The Code:

app.post('/bios/approve', isLoggedIn, function(req, res) {

for (var i = 0; i < req.user.accessLevel.length; i++) {
    if (req.user.accessLevel[i] === "Bio Officer") {

        Bio.findOneAndUpdate({userID: req.body.userID, bioForSector: req.body.bioForSector}, {
            background: req.body.background,
            experience: req.body.experience,
            skills: req.body.skills,
            bioStatus: req.body.bioStatus
        }, function(err, editedBio) {

            if (err)
                console.log("Error while editing Pending Bio is " + err);

            else if (editedBio) {
                User.findOneAndUpdate({accessLevel: "Bio Designer"}, {
                    $push: {biosPending: editedBio._id}
                }, function(err, user) {

                    if (err) {
                        console.log("The error while finding lineManager is " + err);
                    } else if (user) {User.findOneAndUpdate({accessLevel: "Bio Officer"}, {
                            $pull: {
                                biosPending: editedBio._id
                            }
                        }, function(err, bioOfficer) {
                            if (err) {
                                console.log("The error while finding lineManager is " + err);
                            }
                            res.json("Bio Done!")
                        });

                    }
                });

            }

        });

    } else if (req.user.accessLevel[i] === "Bio Designer") {
        // Currently Empty

    } else {

        Bio.findOneAndUpdate({userID: req.body.userID,bioForSector: req.body.bioForSector}, {
            background: req.body.background,
            experience: req.body.experience,
            skills: req.body.skills,
            bioStatus: req.body.bioStatus
        }, function(err, editedBio) {

            if (err)
                console.log("Error while editing Pending Bio is " + err);

            else if (editedBio) {
                User.findOneAndUpdate({accessLevel: "Bio Officer"}, {$push: {biosPending: editedBio._id}
                }, function(err, user) {

                    if (err) {
                        console.log("The error while finding lineManager is " + err);
                    } else if (user) {

                        User.findOneAndUpdate({email: editedBio.lineManagerEmail}, {$pull: {biosPending: editedBio._id}
                        }, function(err, bioOfficer) {

                            if (err) {
                                console.log("The error while finding lineManager is " + err);
                            }
                            res.json("bio Done!")
                        });

                    }
                });

            }
        });
    }
}
});

Any help will be greatly appreciated. Does anyone know what am I doing wrong?

Can't Set Headers After they are sent

means you are sending response multiple times for a single request.

From you code what i can suggest is:

for (var i = 0; i < req.user.accessLevel.length; i++) {
     if(--req.user.accessLevel.length == 0){
        res.json("Bio Done!")
     }
}

First try add res.End(); after res.json(). If that doesn't work can you please add the code of 'isLoggedIn'?

Every time you send back a response you should use the return word too. You want to return to make sure no code after the line gets executed and send another response again accidentally.

Eg: return res.json("bio Done!")

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