【问题标题】:The variable inside of node-horseman evaluate is undefinednode-horseman evaluate 内部的变量未定义
【发布时间】:2017-02-08 03:36:33
【问题描述】:

我的应用程序具有 API 端点,其中一个用于删除。唯一的data-id 由参数/feeds/:feedID 传递,该参数访问Controller 类,将参数值赋予Operation 类的变量feedID。问题:horseman 实例内部的变量 feedID 变为 undefined。代码:

控制器:在访问端点时执行。它抓取参数值并存储在 Operation 类的变量中以供进一步使用

* destroy(request, response) {
  const googleAlertsOperation = new GoogleAlertsOperation()

  googleAlertsOperation.feedID = request.param('feedID')
  ...

操作:使用新的feedID 值,它应该继续执行

* deleteRSSFeed() {
  var feedID = this.feedID //<-- Has the value passed from the Controller

  return new Promise((resolve, reject) => {
    horseman
      .userAgent(this.userAgent)
      .open('https://google.com/alerts')
      .evaluate(function() {
        try {
          return '#manage-alerts-div li[data-id="' + feedID + '"]' //<-- Undefined
        } catch(e) {
          reject(e)
        }
      }).then(function(data) {
        console.log(data)
      })
      .close()
  }).catch((err) => {
    this.addError(HTTPResponse.STATUS_INTERNAL_SERVER_ERROR, reject(err))

    return false
  })
}

我在没有实例化新 Promise 的情况下进行了尝试,但同样的问题。不是传递在顶部创建的变量feedID,而是传递this.feedID 也会变成undefined

【问题讨论】:

  • 是诅咒吗??!!!!
  • 是的。至少我发现了问题,需要提出一个更具体的新问题。道歉。
  • 我觉得你是兄弟!有时代码只是想让你的头发变白!
  • 我还有另外两种方法可以使用相同的方式通过参数传递数据,但在这种情况下它失败了。
  • this.feedID 必须未定义。尝试记录它以查看其价值!

标签: javascript jquery node.js adonis.js node-horseman


【解决方案1】:

我想通了。我必须按照documentation 的示例进行操作:

.evaluate(function(selector) {
  return 'li[data-id="' + selector + '"]'
}, this.feedID)
.then(function(data) {
  console.log(data)
})

注意参数selector。就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多