paulomcnally/jt400.js

Second Execute Call Fails With http.js:689 Error

Closed this issue · 7 comments

I've got it installed and working, but my problem is with the fact that when I try a second SQL statement execution, I'm geting an error about not able to set headers. This is from the second GET call, and you can see the "Entries returned: ###" from both calls, but the second call fails when sending the response.
image

Here's the extracted piece of my end point; config is defined at the beginning of my routes main.js file. I've also tried (started) with the initialize outside the individual route GET block, but moved it here for (I hoped) a separate connection. I'm not as familiar with event emitting as I would like to be.

app.get("/api/v1/:someRouteParam)", function(req, res, next) {
        //proper api call

        ibmdb.initialize(conOb);

        var myVar = someRouteParam
        var qStr = "SELECT * FROM tablename WHERE fieldname='"+myVar+"'";

        ibmdb.execute(qStr);

        //for measuring execution time
        var start = new Date();

        ibmdb.on('execute', function(error, rows){
            if( error ){
                console.log(error);
                res.json({
                    "error" : true,
                    "errorMessage": error,
                    "data": null
                });
            }else{
                console.log("Entries returned: "+rows.length);
                res.json({
                    "error":false,
                    "data": rows
                });
            }
            var end = new Date() - start;
            console.log("Execution time: "+end+"ms");
        });

    });

Try removing "var end = ...". Or added before "res.json..."

I removed the end Date line (and the console.log line) and am getting the same error.

I've also tried adding res.end() after my response, but that doesn't seem to change anything either.

I'm using Express 4 (4.10.1), in case I didn't mention that earlier.

I try install db2 on docker to make tests.

Imgur

Please wait :/

The second execution add the event listener again, so there are really two. On second execution, the first event listener sends the response and console log message then the same function execute again because of the second listener and when you attempt to send results, the headers have already been sent as part of the first "pass".

Change you event listener to:
ibmdb.on('execute', function _someName(error, rows){

Then as the last line of the function:
ibmdb.removeListener('execute', _someName);

Excellent. Thanks for the insight, that definitely did the trick.