0

Everything is executing fine on first run. But, it gives the callback already called error on the second run. Thanks.

Server Side Code:

async.waterfall([
    function(pcallback) {
        //var mykeyword = "";
        //resultset="";

        app.post('/login',function(req,res){
        Keyword=req.body.keyword;
        Category=req.body.category;
        res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000'); // Website you wish to allow to connect
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request methods you wish to allow
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Request headers you wish to allow
        res.setHeader('Access-Control-Allow-Credentials', true); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions)

        console.log("\nKeyword = "+Keyword+", Category is "+Category); //till here everything is executing fine on 2nd run
        //mykeyword=user_name;
        res.end("yes");
        pcallback(null, Keyword);
    });



    },
    function(mykeyword, pcallback) {

        Keyword=mykeyword;
        console.log("\n\nmy keyword ",Keyword)
        callAWS(Keyword, function(response){
            // Here you have access to your variable
            console.log(response);
            pcallback(null,response);
        })

    }, function(resultset, pcallback){
        app.use(function (req, res) {
            res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000/data'); // Website you wish to allow to connect
            res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request methods you wish to allow
            res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Request headers you wish to allow
            res.setHeader('Access-Control-Allow-Credentials', true); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions)
            //console.log(resultset)
            //res.send(resultset)
            //console.log(resultset);
            res.send(resultset)
            res.end();
        });
        pcallback();
}], function(err){
    if(err) return(pcallback(err));
    console.log("I'm Done !");
});

From client side, I've a text box and a button which queries a certain keyword.

error details

1 Answers1

1

I see what's wrong:

When you (first) execute async.waterfall(...) you register a route for POST /login, and in your route handler you call pcallback() every time someone posts to /login :

async.waterfall([
    function(pcallback) {
       // ...
       app.post('/login',function(req,res){ 
          // ... 
          pcallback(null, Keyword);  // <= bug is here
          // ...

async.waterfall expects every callback to be called only once, so the first time a client posts - everything "works", but when a second POST comes along you call pcallback() again unexpectedly.

I'm not sure of what you intended doing, but I think the fix might be moving your entire async.waterfall() code inside the app.post('/login',function(req,res){ }); handler, instead of having it the other way around.

Something like this (if I understood currently):

app.post('/login',function(req,res){
    Keyword=req.body.keyword;
    Category=req.body.category;
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000'); // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Credentials', true); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions)

    console.log("\nKeyword = "+Keyword+", Category is "+Category); //till here everything is executing fine on 2nd run
    //mykeyword=user_name;
    res.end("yes");

    // pcallback(null, Keyword); <== pcallback() was HERE before
    async.waterfall([
        function(pcallback) {
            // Keyword=mykeyword;
            console.log("\n\nmy keyword ",Keyword)
            callAWS(Keyword, function(response){
                // Here you have access to your variable
                console.log(response);
                pcallback(null,response);
            })

        }, function(resultset, pcallback){
            app.use(function (req, res) {
                res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9000/data'); // Website you wish to allow to connect
                res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); // Request methods you wish to allow
                res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); // Request headers you wish to allow
                res.setHeader('Access-Control-Allow-Credentials', true); // Set to true if you need the website to include cookies in the requests sent // to the API (e.g. in case you use sessions)
                //console.log(resultset)
                //res.send(resultset)
                //console.log(resultset);
                res.send(resultset)
                res.end();
            });
            pcallback();
        }], function(err){
        if(err) return(pcallback(err));
        console.log("I'm Done !");
    });

});

Also, I suspect you might have another hidden bug, in your last handler:

Your handler is

function (err) {
    if (err) return (pcallback(err));
    console.log("I'm Done !");
}

but there isn't any pcallback parameter (and there shouldn't be one), so unless you're referring to another pcallback hidden above this snippet, you'll probably get an exception when you actually have an err there.

Hope this helps :)

Yoav Aharoni
  • 2,672
  • 13
  • 18