Reputation: 3
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
};
});
Upvotes: 0
Views: 1294
Reputation: 2092
I would love for this to be a comment but first try to
response.write("some random string without the writeHead...")
if that works try:
response.write(id.**toString()**)
still without the response.writeHead if that works try:
response.write(id.toString())
you may also try JSON.stringify(id)
but these are just strange solutions... I would really want to see all the code you have, for example where is your
var express = require("express");
var ejs = require("ejs");
var app = express();
app.set("view_engine", "ejs"); //or some other view engine like mustache
or at least: var http = require('http'), var util = require('util');
I'm not familiar with that specific NPM (formidable) but by checking out its documentation quickly I did not seem to notice that includes things like response so how is response defined? Sorry if this does not lead you to the correct response but going by what I see as potential problems. You could also try what the other commenter said to try to fix your non string problem if that's the problem.
Also anything after response.end I believe won't be seen. Try to console.log ( before that)... also you can just use response.end(id---if id was a string)
rather than response.write("this string");
then response.end();
response end can include a response write.
Upvotes: 0
Reputation: 1046
The data of id
is probably not a String or a Buffer.
Try this: response.write('' + id);
Upvotes: 4