I'm confused a lot. I receive POST data from browser, parse it and insert it in mysql. Everything works perfect, except the response to client after i inserted data. Response object is visible and it has method 'write' that simply refuses to work.
My code is below, thanks in advance.
var formidable = require("formidable");
var form = new formidable.IncomingForm();
form.parse(request, function (error, fields, files) {
if ( 'create' in fields ) {
args.name = fields.create;
if ( args.name == '' ) {
response.writeHead( 500, {"Content-Type": 'text/html' } );
response.end("this will work");
};
db.myFunction(args, function (id) {
console.log(id); // shows correct data returned from function
response.writeHead( 200, {"Content-Type": "text/html" } );
console.log(response.headersSent); // true
response.write(id); // but this will not work
response.end();
console.log('done'); // at this point node is still silent
});
} else {
response.writeHead( 500, {"Content-Type": 'text/html' } );
response.end("no create in fields"); // works fine
};
});
console.log('done')also isn't working? FWIW, you probably want to do areturnafterresponse.end("this will work").console.log('done')isn't working there might be some issue that will (also) cause yourresponse.write()not to work.function myFunction (args, callback) { connection.query("INSERT INTOtable` (name) VALUES ('"+args.name+"') ", function (err, rows, fields) { if (err) { console.log(err); }; var id = rows.insertId; callback(id); }); }`typeof id? If it's not a string or Buffer, you should do something likeresponse.write(''+id);