0

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
    };
});
6
  • Are you saying that the console.log('done') also isn't working? FWIW, you probably want to do a return after response.end("this will work"). Commented Apr 11, 2015 at 13:59
  • @robertklep Actually I want to send back a chunk of data back to browser. I'm not sure if I correctly understood your statement about placing 'return', since I'm not a native english speaker. Commented Apr 11, 2015 at 14:11
  • I understand, but if the console.log('done') isn't working there might be some issue that will (also) cause your response.write() not to work. Commented Apr 11, 2015 at 14:13
  • The only thing could cause something is my database function. The code is below. function myFunction (args, callback) { connection.query("INSERT INTO table` (name) VALUES ('"+args.name+"') ", function (err, rows, fields) { if (err) { console.log(err); }; var id = rows.insertId; callback(id); }); }` Commented Apr 11, 2015 at 14:17
  • 1
    What is typeof id? If it's not a string or Buffer, you should do something like response.write(''+id); Commented Apr 11, 2015 at 14:18

2 Answers 2

4

The data of id is probably not a String or a Buffer. Try this: response.write('' + id);

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, yeah. It was my silly mistake. Worked for me perfectly. Thanks. :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.