Node Express js Unhandled rejection Error [ERR_HTTP_HEADERS_SENT] - node.js

I am new for node js. I am trying to insert the data to database, before that I did some validation for checking username already exist in database. Please see the coding which is used by me.
var express = require("express");
var router = express.Router();
var models = require("../models");
const { check, validationResult } = require('express-validator/check');
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource - merchant');
});
/* POST ADD USERS. */
router.post('/add-merchant', [
check('name').not().isEmpty(),
], (req, res, next) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json({ errors: errors.array()});
}
console.log("error passed")
models.merchants.findOne({ where: {name: req.body.name}}).then((merchant) => {
if(merchant) {
throw new Error("Username already exists")
}
models.merchants.create(req.body).then((merchant) => {
res.json(merchant)
});
})
} catch (error) {
res.json({"status": "error", "message": error.message})
}
});
module.exports = router;
I am getting below error while running the above code
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/assistanz/Desktop/node/gb360/node_modules/express/lib/response.js:267:15)
at models.merchants.create.then (/home/assistanz/Desktop/node/gb360/routes/merchant.js:33:21)
at tryCatcher (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/assistanz/Desktop/node/gb360/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
Please help anyone to solve this issue.
Thanks in advance.

this error occurs because you return the response and something is remain to load
you have to remove console.log("error passed") or you can put it before return statement..
and write return statement for every res.json statement
as
return res.json({})

HTTP uses a cycle that requires one response per request. When the client sends a request the server should send only one response back to client. So modify the code as below,
models.merchants.findOne({ where: { name: req.body.name } }).then((merchant) => {
if (merchant) {
throw new Error("Username already exists")
} else {
models.merchants.create(req.body).then((merchant) => {
res.json(merchant)
});
}
})

Related

Error: Cannot set headers after they are sent to the client

I code Promise.all , it load about 20 lines from db when running, it stops
My code
router.get('/', (req, res) => {
var cat = res.locals.lcCategories;
Promise.all([postModel.getTop10View(),postModel.getTop10latest()]).then(data => {
console.log(data);
return res.render('home');
}).catch(next);
});
Error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (C:\Users\tranh\Desktop\Web2-News\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\Users\tranh\Desktop\Web2-News\node_modules\express\lib\response.js:170:12)
at done (C:\Users\tranh\Desktop\Web2-News\node_modules\express\lib\response.js:1004:10)
at Immediate.<anonymous> (C:\Users\tranh\Desktop\Web2-News\node_modules\express-handlebars\lib\utils.js:26:13)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
I had the same issue, basically this error occurs when your api as already sent a response , but due to nodejs asynchronus ability or lost cycle it again sends a response , i think that promise.all is run and the request is still sent and when the promise.all returns from its loop it again sends a request.
Use await before promise.all
like
await Promise.all([postModel.getTop10View(),postModel.getTop10latest()]).then(data => {
console.log(data);
return res.render('home');
}).catch(next);

Google Cloud Function - Storage - Delete Image - "ApiError: Error during request"

UPDATED QUESTION
The problem is ApiError: Error during request.
Code:
import * as functions from 'firebase-functions';
const cors = require('cors')({ origin: true });
import * as admin from 'firebase-admin';
const gcs = admin.storage();
export const deleteImage = functions.https.onRequest((req, res) => {
return cors(req, res, async () => {
res.set('Content-Type', 'application/json');
const id = req.body.id;
const name = req.body.name;
const imageRef = gcs.bucket(`images/${name}`);
if (!name || !id) {
return res.status(400).send({message: 'Missing parameters :/'});
}
try {
await imageRef.delete();
console.log('Image deleted from Storage');
return res.status(200).send({status: 200, message: `Thank you for id ${id}`});
}
catch (error) {
console.log('error: ', error);
return res.status(500).send({message: `Image deletion failed: ${error}`});
}
});
});
And the problem is here: await imageRef.delete();, I get the following error:
ApiError: Error during request.
I do, indeed, have admin.initializeApp(); in one of my other functions, so that can't be the issue, unless GCF have a bug.
More In-Depth Error:
{ ApiError: Error during request.
at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/common/src/util.js:187:32)
at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/common/src/util.js:131:18)
at /user_code/node_modules/firebase-admin/node_modules/#google-cloud/common/src/util.js:496:12
at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/#google-cloud/common/node_modules/retry-request/index.js:198:7)
at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:185:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1161:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: undefined,
errors: undefined,
response: undefined,
message: 'Error during request.' }
(old question removed)
"Error: Can't set headers after they are sent" means that you tried to send two responses to the client. This isn't valid - you can send only one response.
Your code is clearly sending two 200 type responses to the client in the event that imageRef.delete() fails and the catch callback on it is triggered.
Also, you're mixing up await with then/catch. They're not meant to be used together. You choose one or the other. Typically, if you're using await for async programming, you don't also use then/catch with the same promise. This is more idiomatic use of await with error handling:
try {
await imageRef.delete()
res.status(200).send({status: 200, message: `Thank you for id ${id}`});
} catch (error) {
res.status(500).send({message: `Image deletion failed: ${err}`});
}
Note also that you typically send a 500 response to the client on failure, not 200, which indicates success.

Cannot set headers after they are sent to the client - Node/Mongoose/Express

I am working on an fantasy soccer app for my friends and I, but I am getting this age old error and it doesn't seem like any of the other questions answered on here really fit my situation. In the first block of code below, the console.log is returning the correct data, so I am very certain that the res.json(populatedClub) should be working just fine. I cannot find anywhere else in my code that is triggering another res.send() or res.json() in this chain of events.
Is anybody else able to see what I am not?
My route:
fantasyClubRouter.get('/:userId',
(req, res) => {
FantasyClub
.findOne({manager: req.params.userId})
.populate({
path: 'manager',
model: 'User'
})
.exec((error, populatedClub) => {
if (error) {
return () => {throw new Error(error)};
}
console.log('populatedClub:', populatedClub);
res.json(populatedClub);
})
.catch(error => {
throw new Error(error);
});
}
);
The error stack:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:471:11)
at ServerResponse.header (/home/ubuntu/workspace/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/ubuntu/workspace/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/home/ubuntu/workspace/node_modules/express/lib/response.js:267:15)
at FantasyClub.findOne.populate.exec (/home/ubuntu/workspace/server/fantasyClub-routes.js:18:11)
at /home/ubuntu/workspace/node_modules/mongoose/lib/model.js:4187:16
at (anonymous function).call (/home/ubuntu/workspace/node_modules/mongoose/lib/query.js:3128:7)
at process.nextTick (/home/ubuntu/workspace/node_modules/mongoose/lib/query.js:2019:28)
at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:
at /home/ubuntu/workspace/node_modules/mongoose/lib/model.js:4189:13
at (anonymous function).call (/home/ubuntu/workspace/node_modules/mongoose/lib/query.js:3128:7)
at process.nextTick (/home/ubuntu/workspace/node_modules/mongoose/lib/query.js:2019:28)
at process._tickCallback (internal/process/next_tick.js:61:11)
I figured out what I had done wrong, and it was so simple that I'm almost embarrassed to admit it, but here it goes.
I updated from an older version of Mongoose and forgot to update my code accordingly. Always make sure your code matches what the docs say for the version of the library you are using.
This is the working code:
fantasyClubRouter.get('/:userId',
(req, res) => {
FantasyClub
.findOne({manager: req.params.userId})
.populate({
path: 'manager',
model: 'User'
})
.then(populatedClub => {
res.json(populatedClub);
})
.catch(error => {
throw new Error(error);
});
}
);

Google Trends API npm quota exceeded

I am using Google Trends API to get historical trends data. I am reading keywords from a CSV file and firing google trends query.
Here is the code
var googleTrends = require('google-trends-api');
var fs = require('fs')
fs.readFile('merged.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
data = data.toString().split("\n");
recur(0, data);
});
function recur(index, data){
if (index < data.length){
var keyword = data[index].split(",")[0];
console.log(keyword);
googleTrends.trendData(keyword)
.then(function(results){
console.log(results);
index = index+1;
recur(index, data);
})
.catch(function(err){
console.error('We have an error!', err);
});
}
}
I get this error many times in between
[ Error: Quota limit exceeded, try again later
at parseJSON (/Users/shubhamjindal/Development/domain-info/node_modules/google-trends-api/lib/resources/htmlParser.js:48:9)
at /Users/shubhamjindal/Development/domain-info/node_modules/google-trends-api/lib/utils/trendData.js:26:11
at tryCatcher (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/promise.js:504:31)
at Promise._settlePromise (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/promise.js:561:18)
at Promise._settlePromise0 (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/promise.js:606:10)
at Promise._settlePromises (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/promise.js:685:18)
at Async._drainQueue (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/async.js:138:16)
at Async._drainQueues (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/async.js:148:10)
at Immediate.Async.drainQueues (/Users/shubhamjindal/Development/domain-info/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:570:20)
at tryOnImmediate (timers.js:550:5)
at processImmediate [as _immediateCallback] (timers.js:529:5) ]
Can someone suggest me some workaround this error?

Rejected promise is not reaching catch

I'm trying to use this method for non-sequential promise output.
The express .json call successfully sends a 201 alongside the user object from the API, but in the console, I get the Unhandled rejection error shown below. This seems like it should be caught by the .catch handler in the controller. I am wondering why this could be happening?
userController
module.exports.postUser = function (req, res, next) {
var user = req.body.user;
var ip = req.ip;
userService.createUser(user, ip)
.then(function (user) {
res.status(201).json({"user": user.toJSON()});
})
.catch(function (err) {
return next(err);
});
};
userService
module.exports.createUser = function (user, ip) {
var user = new Promise(function (resolve, reject) {
return resolve(User.forge(user));
});
return user.then(function validateUser(user) {
return user.validate({validatePassword: true});
})
.then(function hash() {
var password = user.value().get('password');
return hashPassword(password);
})
.then(function setPassword(hashedPass) {
user.value().set('hashedPass', hashedPass);
return user.value().save();
})
.then(function () {
return user;
});
};
output
Unhandled rejection error: null value in column "status" violates not-null constraint
at Connection.parseE (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
From previous event:
at Client._query (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/dialects/postgres/index.js:122:12)
at Client.query (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/client.js:127:24)
at Runner.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:118:24)
From previous event:
at /Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:44:21
From previous event:
at Runner.run (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/runner.js:30:20)
at QueryBuilder.Target.then (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/knex/lib/interface.js:27:43)
at null.<anonymous> (/Users/lukel99/webstorm-workspace/rocketwagon-postgres-kickstart/node_modules/bookshelf/lib/model.js:208:36)
at processImmediate [as _immediateCallback] (timers.js:367:17)
The most likely cause is that return user.value().save() does not return a promise, but implements callbacks instead. If that is the case, then the error would be thrown outside of the native promise try/catch block, and would hence not be caught but your .catch().

Resources