Cant set headers after they are sent node.js - node.js

I am trying to combine multiple textfiles,convert them in a single zip file using zip archiver.
exports.downloadFilesInZip = function(req, res, next) {
var respObj = {};
var file_names = [];
var projectId = 111;
var file_ids = 11111;
console.log(projectId);
db.getConnection(function (err, connection) {
if (err) {
debug(err);
next(err);
}
else {
var updateQuery = "select data from file_data where file_id IN (?)";
console.log(updateQuery);
connection.query(updateQuery,[file_ids], function (err, results) {
console.log("inside" + updateQuery);
if (err) {
connection.release();
console.log("error" + JSON.stringify(err));
debug(err);
next(err);
}
else {
async.eachSeries(results,function(item,loopCallBack){
var text = "";
console.log("hllllllll");
console.log(item.data);
console.log(JSON.parse(item.data));
document_text = JSON.parse(item.data);
console.log("dssddssdsdsdsdsd"+document_text);
for(var j=0; j < document_text.length ;j++)
{
text += document_text[j]['text'];
}
//file_names.push(convertStringToTextFile(text));
convertStringToTextFile(text,function(err,file_name){
if(err){
console.log(err);
loopCallBack(err);
}
else {
file_names.push(file_name);
loopCallBack();
}
})
},function(err){
if(err){
console.log(err);
next(err);
}
else {
var updateQuery = "select name from project where id in (?)";
console.log(updateQuery);
connection.query(updateQuery,[projectId], function (err, results) {
console.log("inside" + updateQuery);
connection.release();
if (err) {
console.log("error" + JSON.stringify(err));
debug(err);
next(err);
}
else {
var fileName_link = JSON.stringify(results[0].name);
console.log("projectname"+fileName_link);
convertTextFilesToZip(file_names,fileName_link, function (err, filename) {
if (err) {
console.log(err);
next(err);
}
else {
console.log("filename link" + filename);
res.json({
status: 0,
file_link: filename
});
}
});
}
});
}
});
}
});
}
});
}
}
convertStringToTextFile = function(text,cb){
var json_filename = 'tmp/file_'+uuid.v4().replace('-','')+'.txt';
fs.writeFile(json_filename, text , function (err) {
if (err) {
debug(err);
cb(err);
}
else{
cb(null,json_filename);
}
});
};
convertTextFilesToZip = function(textFiles,file_link,cb){
console.log("textfiles"+textFiles);
var filename = 'reports/'+JSON.parse(file_link)+'_extractedText.zip';
var output = fs.createWriteStream(filename);
output.on('close', function() {
console.log(zipArchive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
zipArchive.on('error', function(err) {
cb(err);
});
zipArchive.pipe(output);
zipArchive.bulk([
{ expand: true, src: textFiles }
]);
zipArchive.finalize();
cb(null,filename);
}
It works okay the first time and after that it throws this error.I have checked other posts in which res is returned twice but i couldn't find it.It says that can't set headers after they are sent.I think the problem is in the convertTextFilesToZip function but i cant seem to pinpoint the exact location which is generating the error.ANy help is appreciated.
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:350:11)
at ServerResponse.header (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/response.js:154:12)
at fn (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/response.js:934:10)
at View.exports.renderFile [as engine] (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/jade/lib/index.js:374:12)
at View.render (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/view.js:93:8)
at EventEmitter.app.render (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/application.js:566:10)
at ServerResponse.res.render (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/lib/response.js:938:7)
at /Users/zeeshandar/Desktop/Agreements_info/agreements_info/app.js:207:13
at Layer.handle_error (/Users/zeeshandar/Desktop/Agreements_info/agreements_info/node_modules/express/li b/router/layer.js:58:5)

Making my comment into an answer since it appears to have led to the solution.
The variable zipArchive is not initialized in convertTextFilesToZip() therefore you are reusing that variable from one function call to the next and that seems unlikely to be the right implementation.
Also, I would expect your method calls to zipArchive to be asynchronous and it doesn't look like your are coding for that since the callback is called before you have any sort of completion notification.

Related

nodeJS - Cannot set property of undefined

i know this question may be asked about 1000 times here but i cant find the error in my script.
I am trying to iterate through 2 arrays to get a name from an city_id and an organisation_id in a each() loop. i would like to write there values to the "mother"-object to get all informations in one place. Here is the code i've written so far:
let express = require('express');
let router = express.Router();
let request = require('request');
let connection = require('../lib/mysql');
router.get('/', function(req, res) {
if(req.session.loggedin === true){
getList((err, finlist) => {
console.log(finlist)
});
} else {
const cssPath = '/stylesheets/style.css'
res.render('login', { cssPath, title: 'Login' });
}
});
function getList(callback) {
var result = [];
connection.query('SELECT * FROM lists ', (err, rows) => {
if(err) throw err;
var r=0;
rows.forEach(function(item) {
result[r] = item;
getCities((err, cty) => {
result[r].city = cty[item.city_id].name;
getOrganisations((err, org) => {
result[r].organisation = org[item.organisation_id].name;
});
callback(result);
});
r++;
});
});
}
function getCities(callBack) {
var result=[];
connection.query('SELECT * FROM cities ', (err, rows) => {
if (err) throw err;
rows.forEach(function (cty) {
result[cty.id] = cty;
});
if (err) {
callBack(err, null);
} else {
callBack(null, result);
}
});
}
function getOrganisations(callBack) {
var result=[];
connection.query('SELECT * FROM organisations ', (err, rows) => {
if(err) throw err;
rows.forEach(function(org) {
result[org.id] = org;
});
if (err) {
callBack(err, null);
} else {
callBack(null, result);
}
});
};
module.exports = router;
I always get the error
TypeError: Cannot set properties of undefined (setting 'city')
at /opt/alarmprocessor/routes/settings.js:53:32
. . .
which is the line result[r].city = cty[item.city_id].name;
King regards for helping me out :)
Tried to set it as an array, as an Object, made console outputs everywhere... seems all to be fine.. Maybe i am too new to NodeJS so it hasnt been clicked in my Head ;D
This error occur because result[r] doesn't exist line 53. It's declared but doesn't "exist" -> it's undefined. If you perform a mere console.log(result[r]); line 52 you will get an undefined, and you can't set properties (like city) to an undefined value.
The quick fix would be to use optional chaining like this:
result[r]?.city = cty[item.city_id].name;
it won't fix your code, it will only stop crashing by ignoring the assignment.

Boolean not set to true node.js

I have this code:
at the beginning of the file I initialized the variable (var loginState = false;)
Why console.log give me false, although I change it to true
try {
const client = new SimpleGraphClient(tokenResponse.token);
const me = await client.getMe();
sql.connect(config, async function (err){
if (err) console.log(err);
var request = new sql.Request();
request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset){
if (err) console.log(err);
console.log(recordset);
if (recordset.recordset.length == 1) {
loginState = true;
} else {
loginState = false;
}
sql.close();
});
});
console.log(loginState);
if (loginState == true) {
await turnContext.sendActivity({
text: 'Work',
attachments: [CardFactory.adaptiveCard(mainmenu)]
});
} else {
await turnContext.sendActivity({
text: 'Dont work',
attachments: [CardFactory.adaptiveCard(internal_login)]
});
}
} catch (error) {
throw error;
}
Put that console.log() inside sql.connect()`, it works the way you are expecting.
sql.connect() is an asynchronous method, so by the time the console.log() happens the sql.connect() doesn't have changed the variable value yet.
loginState = false; // Statement 1
sql.connect(config, async function (err) { // Statement 2
loginState = true;
})
console.log(loginState); // Statement 3
The order of execution of above will be,
Statement 1
Statement 3
Statement 2
So that's why it happens.
Change the code as follows for it to work fine.
try {
const client = new SimpleGraphClient(tokenResponse.token);
const me = await client.getMe();
sql.connect(config, async function (err) {
if (err) console.log(err);
var request = new sql.Request();
request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset) {
if (err) console.log(err);
console.log(recordset);
if (recordset.recordset.length == 1) {
await turnContext.sendActivity({
text: 'Work',
attachments: [CardFactory.adaptiveCard(mainmenu)]
});
} else {
await turnContext.sendActivity({
text: 'Dont work',
attachments: [CardFactory.adaptiveCard(internal_login)]
});
}
sql.close();
});
});
} catch (error) {
throw error;
}
Hope this helps!
The reason is because you are setting the value to variable inside a callback function :
You have to await your query result. The way you are calling the query is not feasible.
Take a look at - https://www.npmjs.com/package/mysql2
Also change this part -
sql.connect(config, async function (err){
if (err) console.log(err);
var request = new sql.Request();
request.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`, async function (err, recordset){
if (err) console.log(err);
console.log(recordset);
if (recordset.recordset.length == 1) {
loginState = true;
} else {
loginState = false;
}
sql.close();
});
});
Change this to something like this :
try{
let connection = await sql.connect(config);
let query1 = await connection.query(`SELECT * FROM tradebot.accounts WHERE username='${username}' AND password='${password}'`);
if (query1[0].recordset.length == 1) {
loginState = true;
} else {
loginState = false;
}
}catch (err){
// do something here
}

How to return value from a file to router layer using nodejs and expressjs

i have above api in TestRouter.js
TestRouter.js
router.get('/all', function(req, resp) {
var data = reportBo.getAll();
console.log(data);
resp.status(200);
resp.send(data);
return resp;
});
i am calling getAll() from TestRouter.js to TestDao.js.
it is working fine and can fetch the data and can print in console. but i am trying to send this result to TestRouter.js and i am trying to print it on console. but it is showing undefined.
TestDao.js
module.exports.getAll = function () {
var connection = myDB.get();
connection.collection('REPORTS').find({}).toArray(function (err, result) {
if (err) {
throw err;
} else {
//console.log(result);
return result;
}
});
};
module.exports.getAll = function (callback) {
var connection = myDB.get();
connection.collection('REPORTS').find({}).toArray(function (err, result) {
if (err) {
callback(err);
} else {
//console.log(result);
callback(null, result);
}
});
};
And in your router:
router.get('/all', function(req, resp) {
reportBo.getAll(function(err, data){
if(err){
resp.status(500);
} else {
resp.status(200);
resp.send(data);
}
});
});
This way of doing things with callbacks is quite common in Node JS. Also, there is a better way called Promises. You can read up on it.

Promises in complex functions

I'm wondering what the best way to handle errors in long functions with promises are?
My function:
module.exports = function(data) {
var deferred = Q.defer();
var config = {};
fs.readdir(path, function(err, files) {
if (err) {
deferred.reject(new Error(err));
}
var infoPath = false;
files.forEach(function(filename) {
if (filename.indexOf('.info') !== -1) {
infoPath = path + '/' + filename;
config['moduleName'] = filename.replace('.info', '');
}
});
if (!infoPath) {
deferred.reject(new Error('Did not find .info-file'));
}
if (files.indexOf(config['moduleName'] + '.module') > -1) {
config.type = 'Modules';
}
else {
deferred.reject(new Error('Unknown project type'));
}
// Parse the info file.
fs.readFile(infoPath, function (err, content) {
if (err) {
deferred.reject(new Error(err));
}
data.config = config;
data.infoContent = content.toString();
deferred.resolve(data);
});
});
return deferred.promise;
};
As far as I understand it this is the way to use Q.defer. But if a error is thrown, I don't want/need it to try the rest of function. Am I missing something or are there a better way to do this?
Rejecting a promise doesn't miraculously stop the function from executing the rest of its code. So after rejecting, you should return from the function:
if (err) {
deferred.reject(new Error(err));
return;
}
Or shorter:
if (err) {
return deferred.reject(new Error(err));
}

Unable to run a function synchronously in nodejs and express

I have used wikipedia-js for this project. This is my code for summary.js file.
var wikipedia = require("wikipedia-js");
var something = "initial";
module.exports = {
wikitext: function(topicname) {
console.log("Inside wikitex funciton :" + topicname);
var options = {
query: topicname,
format: "html",
summaryOnly: false,
lang: "en"
};
wikipedia.searchArticle(options, function(err, htmlWikiText) {
console.log("Inside seararticlefunciton :");
if (err) {
console.log("An error occurred[query=%s, error=%s]", topicname, err);
return;
}
console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
something = htmlWikiText;
});
return something;
},
};
This module I am using in /wiki/:topicname route. The corresponding code in index.js is like this.
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
var first = summary.wikitext(topicname);
res.send("Hello "+first);
});
The problem is, everytime i visit a wiki/some-topic, the last return statement of summary.js executes before htmlWikiText is populated with content. So I always see hello initial on the browser page. Although after sometime it gets printed on terminal due to console.log statement.
So how should I resolve this issue?
I'm not going to try turning this code into synchronous. I'll just correct it to work as an asynchronous version.
You need to pass in callback to wikitext() and return the value in that callback. Here is the revised code of wikitext() and the route that calls it:
var wikipedia = require("wikipedia-js");
module.exports = {
wikitext: function(topicname, callback) {
console.log("Inside wikitex funciton :" + topicname);
var options = {
query: topicname,
format: "html",
summaryOnly: false,
lang: "en"
};
wikipedia.searchArticle(options, function(err, htmlWikiText) {
console.log("Inside seararticlefunciton :");
if (err) {
console.log("An error occurred[query=%s, error=%s]", topicname, err);
return callback(err);
}
console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
callback(null, htmlWikiText);
});
}
};
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
summary.wikitext(topicname, function(err, result) {
if (err) {
return res.send(err);
}
if (!result) {
return res.send('No article found');
}
res.send("Hello "+result);
});
});

Resources