【发布时间】:2017-05-29 18:59:00
【问题描述】:
我正在尝试在 _.map 循环内调用 setState 函数,但循环丢失了属性“this”,我无法在此上使用 setState,因为 this=undefined
cargaDinamica(){
_.map(this.state.redis, function(cache){
obj.url = 'http://localhost:7000/viewlogredis';
ServiceOS(obj)
.then(retorno => {
console.log("aaaa", this);
view = retorno;
this.setState({log : view});
})
.catch(err => {throw new Error(err)});
obj = {url : 'http://localhost:7000' + cache.WebService,
IPRedis: cache.IPDBMaster,
WebService: cache.WebService,
log : log};
console.log("CARGA: ", obj);
ServiceOS(obj)
.then(function(carga) {
console.log('CHEGOU AQUI');
console.log("OK");
})
.catch(err => {throw new Error(err)});
});
},
这是我的反应函数/\
【问题讨论】:
-
通过有界上下文传递函数(){}.bind(this)
标签: javascript node.js reactjs ecmascript-6 underscore.js