Async - Can't set headers after they are sent - javascript

I am new to node and async...
I am getting an error saying I can't set headers after they sent when I am sending a response back to api-ai
Any idea why?
Below is the code for function - getUserFirstName(userId, name, callback):
var name = "";
function getUserFirstName(userId, name, callback) {
console.log('withParams function called');
request({
method: 'GET',
uri: "https://graph.facebook.com/v2.6/" + userId + "?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=" + FB_PAGE_ACCESS_TOKEN
},
function (error, response) {
if (error) {
console.error('Error while userInfoRequest: ', error);
} else {
if(!typeof response.body != 'object'){
var body = JSON.parse(response.body);
name = body.first_name;
callback(null,name);
}else{
name = response.body.first_name;
callback(null,name);
}
}
});
}
Here is the code being executed:
app.post('/webhook/', (req, res) => {
var data = JSONbig.parse(req.body);
var action = data.result.action;
var facebook_message = [];
if(action == "input.welcome"){
var userId = data.originalRequest.data.sender.id;
async.series([
function(callback) {
getUserFirstName(userId, name, callback);
}
], function(err,results) {
if (results != undefined){ // results = "John"
facebook_message = [{
"text":"Heyyyoo. Welcome!"
}]
}else{
facebook_message = [{
"text":"Hey " + results +"! Welcome!" // Hey John! Welcome!
}]
}
res.json({ // line 308 - error here!
speech: "Greetings",
displayText: "Greetings",
"data": {
"facebook": facebook_message
},
source: "webhook"
});
});
}
// BUNCH OF LONG AND MESSY CODES OVER HERE...
return res.status(200).json({
status: "ok"
});
Error
Error: Cant set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/app/node_modules/express/lib/response.js:719:10)
at ServerResponse.send (/app/mode_modules/express/lib/response.js:164:12)
at ServerRespose.json (/app/mode_modules/express/lib/response.js:250:15)
at /app/src/app.js: 308:15
at /app/node_modules/async/dist/async.js:3694:9
at /app/node_modules/async/dist/async.js:356:16
at replenish (/app/node_modules/async/dist/async.js:877.25)
at iterateeCallback (/app/node_modules/async/dist/async.js:867:17)
at /app/node_modules/async/dist/async.js:840:16

Remove the following:
return res.status(200).json({
status: "ok"
});

Related

How to fix "Error: Can't set headers after they are sent" in Express

I have recently been developing a MERN application and I have recently came into the trouble that express is saying that I am setting headers after they are sent.
I am using mongo db and trying to update a user profile.
I have tried to comment out my res.send points to find the issue but I have failed to do so.
Here is my post method for updating the user profile:
app.post("/api/account/update", (req, res) => {
const { body } = req;
// Validating and Checking Email
if (body.email) {
var email = body.email;
email = email.toLowerCase();
email = email.trim();
body.email = email;
User.find(
{
email: body.email
},
(err, previousUsers) => {
if (previousUsers.length > 0) {
return res.send({
success: false,
message:
"Error: There is already another account with that email address"
});
} else {
}
}
);
}
// Validating Names Function
function checkName(name) {
var alphaExp = /^[a-zA-Z]+$/;
if (!name.match(alphaExp)) {
return res.send({
success: false,
message: "Error: Names cannot contain special characters or numbers"
});
}
}
checkName(body.firstName);
checkName(body.lastName);
// Making sure that all fields cannot be empty
if (!body.email && !body.firstName && !body.lastName) {
return res.send({
success: false,
message: "Error: You cannot submit nothing"
});
}
// Getting User ID from the current session
UserSession.findById(body.tokenID, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
return res.send({
success: false,
message:
"Error: Session token is no longer valid, please login to recieve a new one"
});
}
// Deleting the token ID from the body object as user table entry doesnt store tokens
delete body.tokenID;
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(userData.userId, body, function(err, userInfo) {
if (!err) {
return res.send({
success: true,
message: "Success: User was updated successfully"
});
}
});
});
});
This is the call that I am doing to the backend of the site:
onUpdateProfile: function(fieldsObj) {
return new Promise(function(resolve, reject) {
// Get Session Token
const obj = getFromStorage("the_main_app");
// Defining what fields are getting updated
fieldsObj.tokenID = obj.token;
// Post request to backend
fetch("/api/account/update", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(fieldsObj)
})
.then(res => {
console.log("Verify Token - Res");
return res.json();
})
.then(json => {
console.log("Verify Token JSON", json);
if (json.success) {
window.location.href = `/manage-account?success=${json.success}`;
} else {
window.location.href = `/manage-account?success=${json.success}`;
}
});
});
}
Here is my error message that I am getting:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\express\lib\response.js:158:21)
at C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\routes\api\account.js:270:22
at C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\mongoose\lib\model.js:4641:16
at process.nextTick (C:\Users\kieran.corkin\Desktop\Projects\Mern Template Final\mern-cra-and-server\server\node_modules\mongoose\lib\query.js:2624:28)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
Can anyone help me with this?
EDIT
I have changed my code, this seems to now work however I feel like its a little messy when put together. Any refactoring tips?
Code:
app.post("/api/account/update", (req, res) => {
// Preform checks on data that is passed through
const { body } = req;
var messages = {
ExistedUser:
"Error: There is already another account with that email address",
NameFormat: "Error: Names cannot contain special characters or numbers",
BlankInputs: "Error: You cannot submit nothing",
accountLoggedOut:
"Error: Session token is no longer valid, please login to recieve a new one",
successfullyUpdated: "Success: User was updated successfully"
};
var usersFound;
if (body.email) {
var email = body.email;
email = email.toLowerCase();
email = email.trim();
body.email = email;
User.find(
{
email: body.email
},
(err, UserCount) => {
usersFound = UserCount;
}
);
}
function capitalize(text) {
return text.replace(/\b\w/g, function(m) {
return m.toUpperCase();
});
}
if (body.firstName) {
body.firstName = capitalize(body.firstName);
}
if (body.lastName) {
body.lastName = capitalize(body.lastName);
}
//Making sure that all fields cannot be empty
if (!body.email && !body.firstName && !body.lastName) {
return res.send({
success: false,
message: messages.BlankInputs
});
}
// Getting User ID from the current session
UserSession.findById(body.tokenID, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
return res.end({
success: false,
message: messages.accountLoggedOut
});
}
if (userData) {
// Deleting the token ID from the body object as user table entry doesnt store tokens
delete body.tokenID;
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(userData.userId, body, function(err, userInfo) {
if (userInfo) {
if (!usersFound.length > 0) {
return res.send({
success: true,
message: messages.successfullyUpdated
});
} else {
return res.send({
success: false,
message: messages.ExistedUser
});
}
}
});
}
});
});
You're calling res.send() twice. res.send() ends the process. You ought to refactor such that you call res.write() and only call res.send() when you're done.
This StackOverflow link describes the difference in more detail. What is the difference between res.send and res.write in express?
I believe this is happening, as you're trying to send a response after the first / initial response has already been sent to the browser. For example:
checkName(body.firstName);
checkName(body.lastName);
Running this function twice is going to try and yield 2 different "response" messages.
The product of a single route, should ultimately be a single response.
Thanks for all your help on this issue.
Here is my final code that allowed it to work.
I have also tried to "refactor" it too. Let me know if you'd do something else.
app.post("/api/account/update", (req, res) => {
const { body } = req;
console.log(body, "Logged body");
// Defining objects to be used at the end of request
var updateUserInfo = {
userInfo: {},
sessionToken: body.tokenID
};
var hasErrors = {
errors: {}
};
// Checking that there is at least one value to update
if (!body.email && !body.firstName && !body.lastName) {
var blankError = {
success: false,
message: "Error: You cannot change your details to nothing"
};
hasErrors.errors = { ...hasErrors.errors, ...blankError };
} else {
console.log("Normal Body", body);
clean(body);
console.log("Cleaned Body", body);
updateUserInfo.userInfo = body;
delete updateUserInfo.userInfo.tokenID;
}
// Function to check if object is empty
function isEmpty(obj) {
if (Object.keys(obj).length === 0) {
return true;
} else {
return false;
}
}
// Function to remove objects from body if blank
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === "" || obj[propName] === null) {
delete obj[propName];
}
}
}
// Checking and Formatting Names Given
function capitalize(text) {
return text.replace(/\b\w/g, function(m) {
return m.toUpperCase();
});
}
if (body.firstName) {
body.firstName = capitalize(body.firstName);
}
if (body.lastName) {
body.lastName = capitalize(body.lastName);
}
// Checking and formatting email
if (body.email) {
body.email = body.email.toLowerCase();
body.email = body.email.trim();
// Checking for email in database
User.find({ email: body.email }, (err, EmailsFound) => {
if (EmailsFound.length > 0) {
var EmailsFoundErr = {
success: false,
message: "There is already an account with that email address"
};
hasErrors.errors = { ...hasErrors.errors, ...EmailsFoundErr };
}
});
}
// Getting User Session Token
UserSession.findById(updateUserInfo.sessionToken, function(err, userData) {
// Finding User ID using the current users session token
if (userData.isDeleted) {
var userDeletedError = {
success: false,
message:
"Your account is currently logged out, you must login to change account details"
};
hasErrors.errors = { ...hasErrors.errors, ...userDeletedError };
} else {
// Finding the user profile and updating fields that are present
User.findByIdAndUpdate(
userData.userId,
updateUserInfo.userInfo,
function(err, userInfo) {
// userInfo varable contains user db entry
if (err) {
var updateUserError = {
success: false,
message: "Error: Server Error"
};
hasErrors.errors = {
...hasErrors.errors,
...updateUserError
};
}
if (isEmpty(hasErrors.errors)) {
res.send({
success: true,
message: "Success: You have updated your profile!"
});
} else {
res.send({
success: false,
message: hasErrors.errors
});
}
}
);
}
});
});

How to not call context.done() twice in Azure Function when using request() and callback?

I'm writing an Azure Function that makes a call to a Google API using the npm module request. The callback that processes body needs to call context.done() or else context.res is out of scope (if I set it within the callback and then wait to use it at the end of my script through context.done()).
The problem is I'm not understanding how to both handle errors and return a response to the client and use the callback.
Any and all comments on my code are welcome. I want to improve.
var request = require('request');
module.exports = function (context, req) {
if (req.body) {
incoming = req.body
if (incoming.components && incoming.components.city
&& incoming.components.state
&& incoming.components.country) {
// locality = city
// administrative_area = state
var country = incoming.components.country;
var administrative_area = incoming.components.state;
var locality = incoming.components.city;
request.get({
url: "https://maps.googleapis.com/maps/api/geocode/json?components=country:US|locality:city|administrative_area:CA&key=..."
}, function (error, response, body) {
if (error) {
context.log(error);
}
if (!error && response.statusCode == 200) {
context.log(body)
}
context.res = {
status: 200,
body: "asdf"
};
context.done();
});
context.log("here");
} else {
context.res = {
status: 400,
body: 'There was data but not the right kind. Try {"components": {"country": "US","city": "city","state": "CA"}'
};
}
}
else {
context.res = {
status: 400,
body: 'Nothing was received. Try {"components": {"country": "US","city": "city","state": "CA"}'
};
}
context.done();
};
You shouldn't call context.done() at the end of your function. Only call it when you have the result (i.e. when you detected error or inside the callback). A bit refactored and simplified version of your function:
module.exports = function (context, req) {
if (!req.body) {
context.res = {
status: 400,
body: 'Nothing was received. Try {"country": "US"}'
};
context.done();
return;
}
if (!req.body.country) {
context.res = {
status: 400,
body: 'There was data but not the right kind. Try {"country": "US"}'
};
context.done();
return;
}
request.get({
url: "https://www.google.com?q=" + req.body.country
}, function (error, response, body) {
if (error) {
context.log(error);
context.res = {
status: 500,
body: "Unexpected error"
};
} else {
if (response.statusCode == 200) {
context.log(body)
}
context.res = {
status: 200,
body: "asdf"
};
}
context.done();
});
};

.ajax call never returns

I'm using Express and Node. I've got some code that is posting JSON to another service that is adding it in a database.
The logic is doing what it is supposed to do, but the ajax call I am making is never returning, in the 'Network' tab in Chrome Dev tools it always shows as 'Pending' and eventually errors out with net::ERR_EMPTY_RESPONSE.
Can anyone tell me where I am going wrong?
Ajax Call
$.ajax
({
type: "POST",
url: "/order",
contentType: 'application/json',
data: orderDataJson,
success: function () {
alert("success!");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Something went wrong checking out!\n" + textStatus + "\n" + errorThrown);
}
});
This above routes to /order, which in turn posts the data to the other service:
app.post("/order", function(req, res, next)
{
var options = {
uri: endpoints.ordersUrl + "order",
method: 'POST',
json: true,
body: req.body
};
request(options, function(error, response, body) {
if (error !== null )
{
return;
}
if (response.statusCode == 200 && body != null && body != "")
{
if (body.error)
{
res.status(500);
res.end();
return;
}
res.end(); // UPDATED AFTER COMMENT
return;
}
console.log(response.statusCode);
});
});
This is the relevant code in the other service (it's correctly adding the content in the database)
if (request.method == 'POST')
{
switch (path)
{
// Add a new order
case "/order":
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var orderData = JSON.parse(body);
// Insert into orders table
var saleDate = getDate();
var ordersQuery = "INSERT into orders (customerId, saledate)" +
" VALUES (" + orderData.customerId +",'" + saleDate + "')";
db.query(ordersQuery, function(err, result)
{
if (err)
{
throw err;
}
var orderId = result.insertId;
// Insert order details
for (var i=0; i < orderData.order.length; i++)
{
var productId = orderData.order[i].productId;
var quantity = orderData.order[i].quantity;
var orderDetailsQuery = "INSERT into orderdetails (orderID, productID, quantity)" +
"VALUES (" + orderId + "," + productId + "," + quantity +")";
db.query(orderDetailsQuery, function(err, result)
{
if (err)
{
throw err;
}
});
}
});
response.writeHead(200, {
'Access-Control-Allow-Origin': '*'
});
});
break;
Try to add this in your error block:
if (error !== null ) {
res.status(500).send('Internal server error!');
return;
}
I got this fixed. The issue seem to be that express 'middleware' function should have been:
app.post("/order", function(req, res, body)
as opposed to:
app.post("/order", function(req, res, next)
From the expressjs docs:
If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

Parse TypeError cannot read property 'entry' of undefined at main.js at 39:27

I have a swift project in which i want use push notifications. I tried use parse server using a job schedule with .js files. The problem is that when i run the job on job status window i get this error:
"TypeError cannot read property 'entry' of undefined at main.js at 39:27"
Here is my main.js file:
var xmlreader = require('cloud/xmlreader.js');
var url = "http://www.ilsecoloxix.it/homepage/rss/homepage.xml";
function SavePost(title, link){
var PostClass = Parse.Object.extend("Post");
var post = new PostClass();
post.set("title", title);
post.set("link", link);
post.save();
}
function SendPush(title, link){
var query = new Parse.Query(Parse.Installation);
Parse.Push.send({
where: query,
data: {
url: link,
alert: title,
sound: "default"
}
}, {
success: function() {
SavePost(title, link);
response.success("Push sent to everyone");
},
error: function(error) {
response.error("Error sending push: "+error);
}
});
}
Parse.Cloud.job("fetchPosts", function(request, response) {
Parse.Cloud.httpRequest({
url: url,
success: function(httpResponse) {
xmlreader.read(httpResponse.text, function (err, res){
var newPost = res.feed.entry.at(0);
var title = newPost.title.text();
var link = "";
newPost.link.each(function (i, linkObj){
if (linkObj.attributes().rel == "alternate"){
link = linkObj.attributes().href;
}
});
var PostClass = Parse.Object.extend("Post");
var query = new Parse.Query(PostClass);
query.equalTo("link", link);
query.find({
success: function(results) {
console.log(results);
if (results.length == 0){
SendPush(title, link);
} else {
response.error("Post already pushed");
}
}
});
});
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error("Error fetching posts from feed");
}
});
});
How can i avoid this problem?
I am using Fiddler to display xml and find no feed node, should be res.rss.channel
http://www.ilsecoloxix.it/homepage/rss/homepage.xml
Response data res.feed is undefined. You can console.log(res) to figure out what is wrong. Also make sure the signature is correct for xmlreader.read(httpResponse.text, function (err, res), not sure why err is the first parameter of the function, most likely res is the first.
if(res && res.feed && res.feed.entry){
var newPost = res.feed.entry.at(0);
var title = newPost.title.text();
var link = "";
newPost.link.each(function (i, linkObj){
if (linkObj.attributes().rel == "alternate"){
link = linkObj.attributes().href;
}
});
var PostClass = Parse.Object.extend("Post");
var query = new Parse.Query(PostClass);
query.equalTo("link", link);
query.find({
success: function(results) {
console.log(results);
if (results.length == 0){
SendPush(title, link);
} else {
response.error("Post already pushed");
}
}
});
});
}

Node js custom callback function error

I'm trying to make a simple authentication with node js. Because I read user data from a database, I have to make it asynchronous. Here's my function, which checks if authentication is ok:
function auth(req, callback) {
var header = req.headers['authorization'];
console.log(cb.type);
console.log("Authorization Header is: ", header);
if(!header) {
callback(false);
}
else if(header) {
var tmp = header.split(' ');
var buf = new Buffer(tmp[1], 'base64');
var plain_auth = buf.toString();
console.log("Decoded Authorization ", plain_auth);
var creds = plain_auth.split(':');
var name = creds[0];
var password = creds[1];
User.findOne({name:name, password:password}, function(err, user) {
if (user){
callback(true);
}else {
callback(false);
}
});
}
}
And here I call the function:
auth (req, function (success){
if (!success){
res.setHeader('WWW-Authenticate', 'Basic realm="myRealm');
res.status(401).send("Unauthorized");
}else{
if(user!==req.user) {
res.status(403).send("Unauthorized");
}else{
User.findOneAndUpdate({user:userid}, {user:req.body.user, name:req.body.name, email:req.user.email, password:User.generateHash(req.body.password)},
{upsert:true}, function(err, user) {
if(!err) {
res.status(200).send("OK");
}else{
res.status(400).send("Error");
}
});
}
}
});
This gives me error "TypeError: object is not a function", pointing at "callback(false)". I have no idea what could cause this error, as I pass a function as a parameter, and the first log message prints "[function]". Any help would be appreciated.

Categories

Resources