【问题标题】:CasperJS posting only the last item multiple times from my for-loopCasperJS 从我的 for 循环中多次发布最后一项
【发布时间】:2016-02-23 03:16:00
【问题描述】:

CasperJS 很棒,但它不会将我的控制台输出内容发布到我的本地主机。

casper.wait(5000, function () {
    casper.wait(1000, function () {
        casper.then(function(){
            for  (var i = 0 ; i < 10; i++) {
                var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
                console.log(description);
                var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
                console.log(target_date);
                var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
                console.log(target_location);

                console.log(i, description)

                casper.then(function () {

                    casper.open('http://localhost:1337/events', {
                        method: 'post',
                            data:   {
                            'description': description,
                            'target_date':  target_date,
                            'target_location':  target_location,
                        },
                        headers: {
                           "stuff":"stuff"
                        }
                    });
                });
            }
            this.echo('POST ' + i );
        });
    });
});


casper.run();

Console.log 在我想要的时候准确地输出,但它只发布最后一项。我尝试在多个地方添加 casper.wait,但似乎没有帮助!

【问题讨论】:

    标签: javascript loops post phantomjs casperjs


    【解决方案1】:

    CasperJS 中的所有then*wait* 函数都是asynchronous step functionsJavaScript has function-level scope

    这意味着for循环立即执行,几个then()步骤计划在for循环完全完成后执行。此时函数级变量descriptiontarget_date 等都将具有最后一个i 的值,而i 将为10。这是一个通用的JavaScript 示例:JavaScript closure inside loops – simple practical example

    你可以

    • 将两个调用 casper.then()casper.open() 更改为单个调用 casper.thenOpen(),其中循环变量直接传递给函数:

      casper.thenOpen('http://localhost:1337/events', {
          method: 'post',
              data:   {
              'description': description,
              'target_date':  target_date,
              'target_location':  target_location,
          },
          headers: {
             "stuff":"stuff"
          }
      });
      
    • 或通过引入 IIFE“关闭”每次迭代的变量:

      for  (var i = 0 ; i < 10; i++) {
          (function(){
              var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
              console.log(description);
              var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
              console.log(target_date);
              var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
              console.log(target_location);
      
      
              console.log(i, description)
      
      
              casper.then(function () {
      
                  casper.open('http://localhost:1337/events', {
                      method: 'post',
                          data:   {
                          'description': description,
                          'target_date':  target_date,
                          'target_location':  target_location,
                      },
                      headers: {
                         "stuff":"stuff"
                      }
                  });
              });
          })();
      }
      

    【讨论】:

    • Dope,感谢彻底的回应。很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 2021-08-14
    • 1970-01-01
    • 2017-09-08
    • 2010-09-22
    • 2016-05-10
    • 2016-08-25
    相关资源
    最近更新 更多