Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自类方法的异步回调_Javascript_Node.js_Asynchronous_Callback - Fatal编程技术网

Javascript 来自类方法的异步回调

Javascript 来自类方法的异步回调,javascript,node.js,asynchronous,callback,Javascript,Node.js,Asynchronous,Callback,我正在用JavaScript编写一个类,该类将发送一个针对指定URL的HTTP请求,然后返回响应的主体。我不熟悉Node.js和JavaScript,因此,我很难理解Nodejs的回调和异步特性 我编写了获取URL的实际方法,效果很好。结果可通过摩卡测试 class HttpObject { constructor () { this.url = null this.userAgent = null this.body = null } fetchUrl (

我正在用JavaScript编写一个类,该类将发送一个针对指定URL的HTTP请求,然后返回响应的主体。我不熟悉Node.js和JavaScript,因此,我很难理解Nodejs的回调和异步特性

我编写了获取URL的实际方法,效果很好。结果可通过摩卡测试

class HttpObject {
  constructor () {
    this.url = null
    this.userAgent = null
    this.body = null
  }

  fetchUrl (url, userAgent, callback) {
    this.url = url
    this.userAgent = userAgent

    const request = require('request')
    request(this.url, { timeout: 10000 }, function (error, response, body) {
      if (!error && response.statusCode === 200) {

        //the next line doesn't work, the 'body' field in Mocha test is null
        this.body = response.body
        return callback(response, false)
      } else {
        return callback(null, error)
      }
    })
  }
}
但是当我测试HttpObject的body字段时,它仍然被赋值为
null
。不过,它应该被指定为结果的主体

it('should send and http request to https://www.google.com', function (done) {
  httpObj.fetchUrl('https://www.google.com', 'Mozilla/5.0', (res, err) => {
    assert.strictEqual(httpObj.getUrl(), 'https://www.google.com')
    assert.strictEqual(httpObj.getPort(), 80)
    assert.strictEqual(httpObj.getUserAgent(), 'Mozilla/5.0')

    // previous tests pass, but the following doesn't
    assert.notStrictEqual(httpObj.getBody(), null)

    done()
  })
})

问题是当从创建该函数的类外部调用函数时,上下文
this
将丢失。(主要是回拨)

在您的情况下,因为从其他地方调用了
函数(错误、响应、正文)
,所以它不知道
的值

要解决此问题,您可以使用如下函数:

request(this.url, { timeout: 10000 }, (error, response, body) => {
    // you can now set this.body here safely
})
或者您可以在回调上使用
.bind(this)

request(this.url, { timeout: 10000 }, function (error, response, body) {
    // you can now set this.body here safely
}.bind(this))

HttpObject中的
this.body
语句与
this
值的其余部分在不同的范围内,因为它是
请求的回调。尝试在请求的回调上方添加一个
var=this

类HttpObject{
构造函数(){
this.url=null
this.userAgent=null
this.body=null
}
fetchUrl(url、userAgent、回调){
//加上
var=这个;
this.url=url
this.userAgent=userAgent
const request=require('请求')
请求(this.url,{timeout:10000},函数(错误,响应,正文){
如果(!error&&response.statusCode==200){
//将值更改为该值。
that.body=response.body
返回回调(响应,false)
}否则{
返回回调(空,错误)
}
})
}
}