不是典型的“TypeError:Object#<resource>没有方法'push'”问题</resource>

时间:2014-03-18 21:03:24

标签: angularjs angular-resource

我已经搜索了我在这里找到的所有其他答案,我仍然在这里遇到一个奇怪的问题。它是旧的“TypeError:Object#没有方法'push'”错误,你们很多人之前很可能会遇到这样的错误,你们期望一个对象但是得到一个数组,反之亦然。我们目前正在使用Angular 1.1.5,如果这有所作为的话。这是我的服务:

// my Suggestion service
angular.module('suggestionServices', ['ngResource']).factory('Suggestion', ['$resource', 'Utility', function ($resource, Utility) {
return $resource('/appointments/suggestions/json',
  {},
  {
    'query': {
      method: 'POST',
      isArray: true,
      headers: Utility.headers
    }
  });
}]);

当我最初通过路由器中的解析来调用我的查询时,如下所示:

// my controller
var AddAppointmentCtrl = app.controller('AddAppointmentCtrl', [
    '$scope',
    '$rootScope',
    '$q',
    'Utility',
    'suggestions',
    'date',
    'Suggestion',
    function ($scope, $rootScope, $q, Utility, suggestions, date, Suggestion) {

    // lots of other stuff

}

// this is loaded in through the resolve property of my router
AddAppointmentCtrl.getLists = {
    suggestions: ['Utility', '$q', 'Suggestion', function ( Utility, $q, Suggestion ) {
    Utility.loader.start("Loading suggestions...");
    var deferred = $q.defer();

    Suggestion.query({}, function (successData) {
      deferred.resolve(successData);
      console.log(successData);
      Utility.loader.end();
    }, function (errorData) {
      console.log(errorData);
      deferred.reject(errorData);
    });

    return deferred.promise;
  }]
};

这产生了我期望的结果,我的其余代码也按预期执行:

[{"Employees":[{"account_id":"1330","first_name":"Barack","last_name":"Obama"},{"account_id":"1376","first_name":"Joe","last_name":"Biden"}],"Date":"2014-03-19","Grade":66.6666666667,"Appointments":[],"Superlatives":{"isWithinOneWeek":1,"isWithinTwoWeeks":0,"isNotWithinTwoWeeks":0,"isAlreadyWorking":0,"hasGoodAvailability":1,"recentCleaning":0},"Availability":[{"start":"28800","end":"61200"}]}]

但是,当我在我的请求正文中发送一些数据时,通过访问它来更新我的建议数组:

// this is from my controller above, in the "lots of other stuff" area
$scope.suggestionRequest = new Suggestion();
$scope.suggestionRequest.client_id = null;
$scope.suggestionRequest.date = null;
$scope.suggestionRequest.dow = null;
$scope.suggestionRequest.emp_array = [];
$scope.suggestionRequest.team_length = 2;
$scope.suggestionRequest.start = null;
$scope.suggestionRequest.end = null;
$scope.suggestionRequest.$query();

这会引发“TypeError:Object#没有方法'push'”。但是,该呼叫实际上正在工作并在我的网络选项卡中返回:

[{"Employees":[{"account_id":"1375","first_name":"Sarah","last_name":"Palin"},{"account_id":"1368","first_name":"John","last_name":"McCain"}],"Date":"2014-03-22","Grade":60,"Appointments":[],"Superlatives":{"isWithinOneWeek":1,"isWithinTwoWeeks":0,"isNotWithinTwoWeeks":0,"isAlreadyWorking":0,"hasGoodAvailability":1,"recentCleaning":0},"Availability":[{"start":"28800","end":"61200"}]}]

有没有人看到我错过的明显错误?任何提供的帮助将非常感谢!谢谢!

0 个答案:

没有答案