Angularjs Angular.js承诺返回[Object]

Angularjs Angular.js承诺返回[Object],angularjs,angularjs-service,Angularjs,Angularjs Service,我正在调用一个promise,它在服务中返回一个对象,但不知何故在控制器中丢失了 我从我的控制器呼叫: dataService.getSpanishOverviewByID(newItem, api) .then(getSpanishOverviewByIDSuccess) .catch(errorCallback); 在我的服务中,似乎一切正常: function getSpanishOverviewByID(newItem, api) { console.log(newItem + "

我正在调用一个promise,它在服务中返回一个对象,但不知何故在控制器中丢失了

我从我的控制器呼叫:

dataService.getSpanishOverviewByID(newItem, api) 
.then(getSpanishOverviewByIDSuccess)
.catch(errorCallback);
在我的服务中,似乎一切正常:

function getSpanishOverviewByID(newItem, api) { 
console.log(newItem + " / " + api);
return $http({
  method: 'GET',
  url: '/api/' + api,
  newItem: newItem
})
.then(getSpanishByIDSuccess)
.catch(sendGetVolunteerError);
}


function getSpanishByIDSuccess(response) {
    var log = [];  
    angular.forEach(response.data, function(item, key) {
          console.log('I found this!' + response.config.newItem + " " + item.title);
          if(item.title == response.config.newItem){
            console.log('matching ' + item);
            this.push(item);
        }
    }, log);
    console.log(log);
    return log; 
  }
function getSpanishOverviewByIDSuccess(data) {
  $rootScope.newItem = data;
  console.log('brought back ' + data);
}
console.loglog返回一个对象:

由于某些原因,这无法正确传递给控制器:

function getSpanishOverviewByID(newItem, api) { 
console.log(newItem + " / " + api);
return $http({
  method: 'GET',
  url: '/api/' + api,
  newItem: newItem
})
.then(getSpanishByIDSuccess)
.catch(sendGetVolunteerError);
}


function getSpanishByIDSuccess(response) {
    var log = [];  
    angular.forEach(response.data, function(item, key) {
          console.log('I found this!' + response.config.newItem + " " + item.title);
          if(item.title == response.config.newItem){
            console.log('matching ' + item);
            this.push(item);
        }
    }, log);
    console.log(log);
    return log; 
  }
function getSpanishOverviewByIDSuccess(data) {
  $rootScope.newItem = data;
  console.log('brought back ' + data);
}
console.log“带回”+数据;返回:


我不知道为什么会发生这个错误。似乎承诺应该传递console.loglog中返回的数据

这是Javascript字符串连接的一个怪癖。如果您自己记录一个对象,控制台将以您上面截屏的友好方式显示整个对象。但是,如果您记录一个字符串加上一个对象,Javascript解释器会将该对象转换为[object object],而不是正确地将其字符串化。物体本身可能还很好;log语句只是提供了它的类型转换版本

这里最简单的解决方案是:

console.log('brought back');
console.log(data);
或:


然后,您的标识字符串和完整对象及其属性的可视化层次结构将分别记录,这样可以避免字符串连接问题,而且一切看起来都很好。

这是Javascript字符串连接的一个怪癖。如果您自己记录一个对象,控制台将以您上面截屏的友好方式显示整个对象。但是,如果您记录一个字符串加上一个对象,Javascript解释器会将该对象转换为[object object],而不是正确地将其字符串化。物体本身可能还很好;log语句只是提供了它的类型转换版本

这里最简单的解决方案是:

console.log('brought back');
console.log(data);
或:


然后,您的标识字符串和完整对象及其可视化属性层次结构将分别记录,这样可以避免字符串连接问题,而且一切都会很好。

经过一些测试,如果您使用逗号而不是加号,则一条语句就可以了:console.log'bricked back',data;经过一些测试后,如果您使用逗号而不是加号,看起来一条语句就可以了:console.log'bricked back',data;