Angular-UI Modal Error: Expected response to contain an object but got an array

Angular-UI Modal Error: Expected response to contain an object but got an array

首先,我知道这个错误已经在这里被多次提及,我仔细阅读了很多关于这个的问题,但似乎没有一个能完全符合我的问题。为什么?嗯,其中大部分与 Angular UI 模态及其请求显示资源的方式无关。

我将开始向您展示我的控制器:

'use strict';

// Customers controller
angular.module('customers').controller('CustomersController', 
['$scope', '$stateParams', '$location', '$filter', 'Authentication', 'Customers', '$modal', '$log',
    function($scope, $stateParams, $location, $filter, Authentication, Customers, $modal, $log ) {
         // Find a list of Customers
         $scope.find = function() {
            Customers.query(function (data) {
                $scope.customers = data;
                $scope.buildPager();
             });
         };

         // Find existing Customer
         $scope.findOne = function() {
              $scope.customer = Customers.get({ 
                   customerId: $stateParams.customerId
              });
          };

          //List Pager
         $scope.buildPager = function () {
             $scope.pagedItems = [];
             $scope.itemsPerPage = 15;
             $scope.currentPage = 1;
             $scope.figureOutItemsToDisplay();
         };

        /* Modal stuff */

        $scope.animationsEnabled = true;

        $scope.openUpdateModal = function (size, selectedCustomer) {

            var modalInstance = $modal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'modules/customers/views/edit-customer.client.view.html',
                controller: function ($scope, $modalInstance, upcustomer) {
                      $scope.upcustomer = upcustomer;
                },
                size: size,
                resolve: {
                    upcustomer: function () {
                        return selectedCustomer;
                    }
                 }
            });

            modalInstance.result.then(function (selectedItem) {
                $scope.selected = selectedItem;
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
       };

       $scope.toggleAnimation = function () {
           $scope.animationsEnabled = !$scope.animationsEnabled;
       };



//Controller Closing brackets    
    }
]);

在视图中我有一个客户列表,因此如果我单击其中一个我希望打开模式:

<a data-ng-repeat="customer in pagedItems" ng-click="openUpdateModal('lg', customer)" class="list-group-item"></a>

我是根据 Bossable.com 视频教程做这件事的:MEAN Stack: 20 - Pass Customer Details to an Angular UI Modal

唯一不同的是我在点击客户时出现了这个错误:

 [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array

感谢您的宝贵时间。

您的问题与您的 Customer.query() 或 Customer.get() 调用有关。这些是 $resource 调用,期望被告知响应是什么。

通常 get 方法需要一个对象,而 query 方法需要一个数组。如果您不是 return 这些方法之一,您需要使用 isArray 对其进行配置:true/false根据您的需要选择。

可以找到关于此的文档:

https://docs.angularjs.org/api/ngResource/service/$resource

$resource( 'apiendpoint', {}, {
        query: {
            isArray: false
        }
    });
}

只需删除 $scope.findOne 函数,因为它使用了 get 方法和 returns 和数组。如果你回头看她的教程,她一直在评论。