node.js - 我想在运行nightmare.js之后获取gig中的所有数据,但是我一直没有定义gig

标签 node.js nightmare

我想在运行nightmare.js之后获取gig中的所有数据,但是我一直没有定义gig

const Nightmare = require('nightmare');
    const nightmare = Nightmare({show: true});

    nightmare
      .goto('https://www.nairaland.com/romance')
      .wait(1000)
      .evaluate(function(){
        var gig = [];
        var links = document.querySelectorAll('a');
        links.forEach(function(result){
            gig.push(result.innerHTML)
        })
        return gig;
      })
      .end()
      .then(function(result){
       gig.forEach(function(result){
        console.log(gig)
       })
      })
      .catch(error => {
        console.error('The Error is', error)
      })

最佳答案

您对gig = []的声明在您所评估函数的范围内。该函数返回时,它将超出范围。

将其放在外部范围内。也许是这样吗?

const gig = [];
nightmare
  .goto('https://www.nairaland.com/romance')
  .wait(1000)
  .evaluate(function(){
    const links = document.querySelectorAll('a');
    links.forEach(function(result){
...

关于node.js - 我想在运行nightmare.js之后获取gig中的所有数据,但是我一直没有定义gig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55854787/

相关文章:

node.js - 初始化后更改梦Night的可见性

javascript - Promisify Redis 客户端

node.js - Meteor Node 进程 CPU 使用率接近 100%

javascript - 使用 gulp 索引 Nodejs 或 browserify 组件

npm audit fix 未修复低漏洞

javascript - 如何让 Nightmare 强制超时

jquery - 没有从 jQuery 循环获取值

node.js - 预期错误 : Error: Max connection attempts reached

javascript - 我可以将 jest 测试作为常规 js 文件运行吗?

javascript - 是否有可能执行从与梦魇网站的JavaScript编写的功能?或任何其他方式?