Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.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 将数据从promise-then方法传递到对象方法_Javascript_Node.js_Promise_Javascript Objects - Fatal编程技术网

Javascript 将数据从promise-then方法传递到对象方法

Javascript 将数据从promise-then方法传递到对象方法,javascript,node.js,promise,javascript-objects,Javascript,Node.js,Promise,Javascript Objects,如何将数据从promise传递到对象方法 this.httpReq(url).then(function (data) { this.storeData(data); }); 我知道这里我不在范围之内,这个没有引用我的对象。但是,尽管如此,我不明白如何解决它。 下面您可以找到完整的代码片段最后,我想从服务API获取数据,并将其存储在对象数组属性this.storage中 var http = require('http'); function CoubApi (url) { this

如何将数据从promise
传递到对象方法

this.httpReq(url).then(function (data) {
  this.storeData(data);
});
我知道这里我不在范围之内,
这个
没有引用我的对象。但是,尽管如此,我不明白如何解决它。 下面您可以找到完整的代码片段
最后,我想从服务API获取数据,并将其存储在对象数组属性this.storage中

var http = require('http');

function CoubApi (url) {
  this.url = url;
  this.storage = [];
  this.httpReq = httpReq;
  this.searchData = searchData;
  this.storeData = storeData;
}

function httpReq (url) {
  var promise = new Promise (function (resolve, reject) {

    http.get(url, function (res) {
      var data = '';

      res.on('data', function (chunk) {
        data += chunk;
      });

      res.on('end', function () {
        if(data.length > 0) {
          resolve(JSON.parse(data));
        } else {
          reject("Error: HTTP request rejected!");
        }
      });

    }).on('error', function (err) {
      console.log("Error: ", e);
    });

  });

  return promise;
}

function storeData (data) {
  var i;
  console.log("Storrrreee");
  for(i = 0; i < 10; i++) {
    this.storage.push(data.coubs[i]);
  }
}

function searchData (searchtext, order, page) {
  var url = this.url+
            "search?q="+searchtext+
            "&order_by="+order+
            "&page="+page;


  this.httpReq(url).then(function (data) {
    this.storeData(data);
  });
}

var coub = new CoubApi("http://coub.com/api/v2/");
coub.searchData("cat", "newest_popular", 1);
console.log(coub.storage);
var http=require('http');
函数CoubApi(url){
this.url=url;
这是存储=[];
this.httpReq=httpReq;
this.searchData=searchData;
this.storeData=storeData;
}
函数httpReq(url){
var承诺=新承诺(功能(解决、拒绝){
get(url,函数(res){
var数据=“”;
res.on('data',函数(块){
数据+=块;
});
res.on('end',function(){
如果(data.length>0){
解析(JSON.parse(data));
}否则{
拒绝(“错误:HTTP请求被拒绝!”);
}
});
}).on('error',函数(err){
日志(“错误:”,e);
});
});
回报承诺;
}
函数存储数据(数据){
var i;
控制台日志(“Storrrreee”);
对于(i=0;i<10;i++){
this.storage.push(data.coubs[i]);
}
}
函数searchData(搜索文本、订单、页面){
var url=this.url+
“搜索?q=“+searchtext+
“&order_by=“+order+
“&page=“+page;
this.httpReq(url).then(函数(数据){
这个。存储数据(数据);
});
}
var coub=新CoubApi(“http://coub.com/api/v2/");
coub.searchData(“猫”,“最新流行”,1);
控制台日志(coub.storage);

您可以将其存储在变量中:

var self = this;
this.httpReq(url).then(function (data) {
  self.storeData(data);
});
或使用绑定:

this.httpReq(url).then(function (data) {
  this.storeData(data);
}.bind(this));
您可以查看绑定承诺的上下文
。然后()
,或者使用它来捕获所包含上下文的
,以及
搜索数据
。这是假设您的浏览器支持箭头函数。