The variable inside of node-horseman evaluate is undefined

453 Views Asked by At

My application has API endpoints which one of them is for deleting. The unique data-id is passed by parameter /feeds/:feedID, that accesses the Controller class, giving the param value to the Operation class' variable feedID. Issue: the variable feedID becomes undefined inside of horseman instance. Codes:

Controller: Executed when accessing the endpoint. It grabs the parameter value and stores in the Operation class' variable for further use

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

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

Operation: With a new feedID value, it should proceed with its execution

* 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
  })
}

I tried it without instantiating a new Promise, but same issue. Instead of passing a variable feedID, created at the top, passing this.feedID also turns undefined.

1

There are 1 best solutions below

0
AudioBubble On

I figured it out. I had to follow the example at the documentation and make it like this:

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

Notice the parameter selector. That's it.