第二次点击回调错误

callback error on second click

首先 运行 一切正常。但是,它在第​​二个 运行 上给出了回调已经调用的错误。谢谢

服务器端代码:

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 !");
});

在客户端,我有一个文本框和一个查询特定关键字的按钮。

我看看有什么问题:

当你(第一次)执行 async.waterfall(...) 您为 POST /login 注册了一条路线,并且在您的路线处理程序中,每次 某人发帖给 /login

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

async.waterfall 期望每个回调只被调用一次,所以第一次客户端发布时 - 所有 "works",但是当第二个 POST 出现时你调用 pcallback()又出乎意料。

我不确定你打算做什么,但我认为修复 可能 将你的整个 async.waterfall() 代码移动到 app.post('/login',function(req,res){ }); 处理程序中,而不是反过来。

类似这样的事情(如果我目前理解的话):

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 !");
    });

});

此外,我怀疑您在最后一个处理程序中可能还有另一个隐藏的错误:

您的处理人是

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

但是没有任何 pcallback 参数(而且不应该有),所以除非你指的是隐藏在这个代码段上方的另一个 pcallback,否则你可能会得到当你实际上在那里有错误时是一个例外。

希望这对您有所帮助:)