Javascript 将.map函数中的扩展对象作为承诺从服务返回给控制器
我要做的是在服务中扩展JSON对象,然后将其传递给控制器 JSON来自另一个进行后端调用的服务 代码非常复杂,因此我添加了注释和console.log:Javascript 将.map函数中的扩展对象作为承诺从服务返回给控制器,javascript,angularjs,json,promise,Javascript,Angularjs,Json,Promise,我要做的是在服务中扩展JSON对象,然后将其传递给控制器 JSON来自另一个进行后端调用的服务 代码非常复杂,因此我添加了注释和console.log: //get games config object from another service gamesConfig: gamesConfigService.gamesConfig(), // prepare name of games icons. This is support function executed
//get games config object from another service
gamesConfig: gamesConfigService.gamesConfig(),
// prepare name of games icons. This is support function executed in next method
transformSpace: function(subject) {
var ensuredSubject = subject.toString().toLowerCase();
var transformedSubject = ensuredSubject.replace(/ /g, '_');
return transformedSubject;
},
//add iconname property to game config object
extendGameConfig: function() {
var that = this;
this.gamesConfig
.then(function (response) {
console.log(response.data); // this works and console.log my JSON
response.data.map(function(obj) {
return new Promise(function(res){
angular.extend(obj, {
iconname: that.transformSpace(obj.attributes.name) + "_icon.png"
});
});
});
}, function () {
console.log('errror');
});
这包含一个支持方法transformSpace和main方法,该方法未正确传递数据。我想
我试图通过以下方式在控制器中获得此承诺:
theService.getGamesObj.extendGameConfig()
.then(function (response) {
$scope.allGames = response;
console.log($scope.allGames);
}, function () {
console.log('err')
});
然后我会在视图中使用它。目前,上面的代码不起作用,并给我以下错误:
TypeError:无法读取未定义的属性“then”
我在我认为你的代码出错的地方添加了注释
extendGameConfig: function() {
// ***********
// use => functions, that = this wont be needed
var that = this;
// ***********
// if you want this this function to return something, add a return
// this is why you get the
// Cannot read property 'then' of undefined error
// as this function returns undefined
this.gamesConfig
.then(function (response) {
console.log(response.data); // this works and console.log my JSON
// ***********
// you're using .map ... and discarding the result!
response.data.map(function(obj) {
// ***********
// you're creating a promise that never resolves!
// also, why are you promisifying synchronous code?
return new Promise(function(res){
angular.extend(obj, {
iconname: that.transformSpace(obj.attributes.name) + "_icon.png"
});
});
});
}, function () {
console.log('errror');
});
那么,试试这个
extendGameConfig: function() {
return this.gamesConfig
.then(response => {
return response.data.map(obj => {
return angular.extend(obj, {iconname: this.transformSpace(obj.attributes.name) + "_icon.png"});
});
}, function () {
console.log('errror');
});
或者,更好
extendGameConfig: function() {
return this.gamesConfig
.then(response =>
response.data.map(obj =>
angular.extend(obj, {iconname: this.transformSpace(obj.attributes.name) + "_icon.png"})
)
)
.catch(function (err) {
console.log('error', err);
throw err; // log the error, but you'll probably want to reject this promise so the calling code doesn't think there is success?
});
}
extendGameConfig函数不返回任何内容。你是说,把这个还给我。游戏配置。然后。。。?这将解释您得到的类型错误。您尚未在此代码中定义gamesConfig。这需要是一个承诺,你必须返回它,以解决以后与一个新的thenAleksey Solovey我确实定义了gamesConfig在这第一个代码之前,我编辑,但无论如何它是来自另一个服务,使调用后端和返回响应。CRice当我添加return时,错误消失了,但当我在控制器中输入console.log响应时,它是未定义的。