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.
E.g.: return res.json("bio Done!")
Related
I've come across a very peculiar bug.
I have a database with the following records:
{
"_id": {
"$oid": "xxxxxxxxxxxxxxxxx"
},
"name": "Name1",
"number": 42
}
{
"_id": {
"$oid": "xxxxxxxxxxxxxxxxx"
},
"name": "Name2",
"number": 123
}
In my javascript code, I have the following code:
MyRecords.find({}, function(err, results) {
if (err) {
console.log("Error finding phone recipients:", err);
} else if (results.length == 0) {
console.log("No users");
} else {
for (var i = 0; i < results.length; i++) {
var result = results[i];
someFunction2("", result);
someFunction1(function(message) {
someFunction2(message, result);
});
result.number = "Some new value"
result.save(function(err) {
if (err) {
console.log("Error updating last sent for", result.name);
} else {
console.log("Updated last sent time for user:", result.name);
}
});
}
}
}
Assume that someFunction1 and someFunction2 make some API calls and wait for results for a few milliseconds.
When I run this, the console prints out:
Updated last sent time for user: Name2
The user Name1 is not processed. What is going on here?
Assume that someFunction1 and someFunction2 make some API calls and wait for results for a few milliseconds.
Hi, and there is exactly your problem. Once someFunction2 and someFunction1 have retrieved the data from the API, the loop has already run and the value of 'result' will be the last one in the loop.
A way to avoid this is to abstract those to a function outside of the loop, which receives 'result' as a parameter like so:
MyRecords.find({}, function(err, results) {
function processResult(result) {
someFunction2("", result);
someFunction1(function(message) {
someFunction2(message, result);
});
result.number = "Some new value"
result.save(function(err) {
if (err) {
console.log("Error updating last sent for", result.name);
} else {
console.log("Updated last sent time for user:", result.name);
}
});
}
if (err) {
console.log("Error finding phone recipients:", err);
} else if (results.length == 0) {
console.log("No users");
} else {
for (var i = 0; i < results.length; i++) {
processResult(results[i]);
}
}
}
Hope it helps
I keep getting this error (after a certain amount of time) when i refresh the my 'members area' after login. I use response.redirect to redirect to the area (success) or back to sign in form (error). How can i fix this?
app.get('/sellers/login', function(request, response) {
if(request.session.sellerId){
response.redirect( '/sellers/area?logged_in=true');
}
else{
response.render('pages/sellers-login');
}
});
app.post('/authenticate', function(request, response) {
if(request.session.sellerId){
response.redirect('/area?logged_in=true');
}
else{
db.authenticate(request.body.loginid, function(err, results) {
if(err){
response.redirect('/sellers/login?err=1&logged_in=false&type=db');
}
else{
if(results.length >=1){
var hash = results[0]['hash'];
var seller_id = results[0]['id'];
bcrypt.compare(request.body.password, hash, function(err, res) {
if(res){
request.session.sellerId = seller_id;
response.redirect('/sellers/area?logged_in=true');
}
else{
response.redirect('/sellers/login?err=1&logged_in=false&type=pMatch');
}
});
}
else{
response.redirect('/sellers/login?err=1&logged_in=false&type=user');
}
}
});
}
});
app.get('/sellers/area', function(request, response) {
if(request.session.sellerId){
response.render('pages/sellers-area');
}
else{
response.redirect('/sellers/login?not_logged_in=true');
}
});
if(request.session.sellerId){
response.redirect('/area?logged_in=true');
}
db.authenticate(request.body.loginid, function(err, results) {
// ....
});
You send the header when request.session.sellerId evaluates to true by calling the response.redirect, but you continue with the the db.authenticate afterwards.
Inside of that callback you will do another redirect, even when your redirect for that response has already taken place.
Inside of the db.authenticate you have the same problem with the if(err)
EDIT
you need to use an else block (or a return) for both the if (request.session.sellerId) and the if (err) {
if (request.session.sellerId) {
response.redirect('/area?logged_in=true');
} else {
db.authenticate(request.body.loginid, function(err, results) {
if (err) {
response.redirect('/sellers/login?err=1&logged_in=false&type=db');
} else if (results.length >= 1) {
var hash = results[0]['hash'];
var seller_id = results[0]['id'];
bcrypt.compare(request.body.password, hash, function(err, res) {
if (res) {
request.session.sellerId = seller_id;
response.redirect('/sellers/area?logged_in=true');
} else {
response.redirect('/sellers/login?err=1&logged_in=false&type=pMatch');
}
});
} else {
response.redirect('/sellers/login?err=1&logged_in=false&type=user');
}
});
}
I think i've found precisely where the error originates from which lies in the db.authenticate
//db.js
exports.authenticate = function(loginid, callback) {
var sql = "select ...";
pool.getConnection(function(err, connection) {
if(err) {
console.log(err);
callback(true);
return;
}
connection.query(sql, [loginid], function(err, results) {
connection.release();
if(err) {
console.log(err);
callback(true);
return;
}
callback(false, results);
});
connection.on('error', function(err) {
console.log(err);
connection.release();
callback(true);
return;
});
});
};
The connection.on('error',... gets called, so the callback(true) activates causing this error.
So I'm trying to create a sign up route that checks to see if the user exists first and i have the database call in a separate function that needs to return true or false when it's done. The problem is i'm not very familiar with callbacks and the whole asynchronous thing everything that i have searched for does not seem to work keeps giving me.
TypeError: callback is not a function
This is my code any help or direction would be appreciated.
function pullUserFromDatabase(username, callback) {
console.log(username); //for debug
mongodb.connect(url, function(err, db) {
if(err) {
console.log("didn't get far" + err) //for debug
}
var collection = db.collection(username);
collection.findOne({username}, function(err, item) {
if(err) {
console.log("nope it broke" + err) //for debug
} else {
console.log("it worked" + JSON.stringify(item)) //for debug
callback(true);
}
});
});
}
app.post("/signup", function(req, res) {
var username = req.headers["username"],
password = req.headers["password"],
randomSalt = crypto.randomBytes(32).toString("hex"),
passwordHashOutput = crypto.createHash('sha256').update(password + randomSalt).digest("hex");
if(!username || !password) {
res.send("Username or password not provided.")
} else if(pullUserFromDatabase(username)) {
res.send("User exist.")
}
});
You need to use the callback as follows:
function pullUserFromDatabase(data, callback) {
console.log(data.username); //for debug
mongodb.connect(url, function(err, db) {
if(err) {
console.log("didn't get far" + err) //for debug
}
var collection = db.collection(data.collection);
collection.find({"username": data.username}).count(function (err, count) {
callback(err, !! count);
});
});
};
app.post("/signup", function(req, res) {
var username = req.headers["username"],
password = req.headers["password"],
randomSalt = crypto.randomBytes(32).toString("hex"),
passwordHashOutput = crypto.createHash('sha256').update(password + randomSalt).digest("hex");
if(!username || !password) {
res.send("Username or password not provided.")
}
var data = {
username: username,
collection: "collectionName"
}
if(!username || !password) {
res.send("Username or password not provided.")
}
pullUserFromDatabase(data, function(err, exists) {
if (err) {
res.send(400, "Error - " + err);
}
else if(exists) {
res.send(200, "User exists.");
}
res.send(200, "User does not exist.");
});
});
The reason that callback is undefined is because you didn't pass a 2nd argument to pullUserFromDatabase(username) Provide a 2nd argument, eg. pullUserFromDatabase(username, function(result) {/* do something here with the result variable */})
If you're not very familiar with aync & callbacks, you might find it more intuitive to use promises, but that comes with its own learning curve.
In the context of the original code, this looks like:
...
if(!username || !password) {
res.send("Username or password not provided.");
return;
}
pullUserFromDatabase(username, function(result) {
if(result) {
res.send("User exist.");
} else {
// TODO: Handle this case. If res.send() is never called, the HTTP request won't complete
}
});
...
Also, you need to ensure your callback is always invoked. Add callback(false):
console.log("nope it broke" + err); //for debug
callback(false);
Do a similar step after "didn't get far" and then return so the callback doesn't get invoked multiple times.
I'm using Angular Fullstack. Could someone please tell me what's wrong with my syntax? I'm not sure the proper way to handle it.
Here's what I'm trying to accomplish. (If Auth.hasRole('admin') is true, I want it to show all orders. If the user is authenticated, I want it to show them only their orders. If the user is not authenticated, I want it to say "Unauthorized".
Here's the code
exports.index = function(req, res) {
if(Auth.hasRole('admin')) {
Order.find(function (err, orders) {
if(err) { return handleError(res, err); }
return res.status(200).json(orders);
});
} else if (!Auth.isAuthenticated) {
return res.status(401).send('Unauthorized');
} else {
Order.find().where({ name: 'someusername' }).exec(function (err, orders) {
if(err) { return handleError(res, err); }
return res.status(200).json(orders);
});
}
}
I'm not getting any errors, but when I access the api/orders it just hangs and times out.
You are missing a return for your first condition.
If you are testing with a user with role 'admin', there won't be a response.
if(Auth.hasRole('admin')) {
Order.find(function (err, orders) {
if(err) { return handleError(res, err); }
return res.status(200).json(orders);
});
}
I'm querying for a user object and performing a series of updates on the items within compared to incoming postdata (jsObject). I'm wondering how to completely remove an item from the object...specifically a Date object (user[0].birthDate)...before I make a save of the updated user object.
orm.User.find({ appId: appId, facebookUsername:usersFBUsername}).exec(function (error, user) {
if (error) {
console.log('Error in User Query: ' + error);
}
else if(Object.keys(user).length > 0) {
if(jsObject.name != null)
user[0].name = jsObject.name;
if(jsObject.email != null)
user[0].emailAddress = jsObject.email;
if(jsObject.birthDate != null && jsObject.birthDate.length > 0) {
user[0].birthDate = jsObject.birthDate;
}
else {
console.log('delete it');
//orm.User.update({_id:user._id}, {$pull:{birthDate:1}});
//delete user[0].birthDate;
}
}
user[0].save(function (error) {
if (error != null) {
console.log('An error has occurred while saving user:' + error);
response.end(results.getResultsJSON(results.ERROR, error));
}
else {
console.log(' [User Successfully Updated]');
response.end('{ "success": ' + JSON.stringify(user[0]) + ' }');
}
});
});
You can see in the commented code some attempts I've made which have not been successful. I even gave this a try after the save completed, which also did not work:
orm.User.update({appId: appId, facebookUsername:usersFBUsername},{$pull:{birthDate:deleteBirthDate}})
.exec(function(error){
if(error) {
console.log('oh well: ' + error);
}
else {
console.log('maybe maybe');
}
});
I appreciate any suggestions.
Chris
$pull is for removing values from arrays, but you can use $unset:
orm.User.update(
{_id : user._id},
{ $unset : { birthDate : 1 }},
function(err, numAffected) {
...
}
);
For reference:
https://docs.mongodb.com/manual/reference/operator/update/unset/