Angular-UI模态错误:期望的响应包含一个对象,但得到一个数组

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

本文关键字:数组 一个 错误 模态 期望 一个对象 包含 响应 Angular-UI      更新时间:2023-09-26

首先,我知道这个错误在这里已经被讨论过很多次了,我仔细阅读了很多关于这个问题的问题,但似乎没有一个完全适合我的问题。为什么?它们中的大多数都与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

感谢您的宝贵时间。

删除$scope.findOne函数,因为它使用get方法并返回一个数组。如果你回头看看她的教程,你会发现她一直在评论。

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

通常get方法需要一个对象,而query方法需要一个数组。如果你没有为这些方法返回其中一个,你需要根据你的需要使用isArray: true/false选项来配置它。

相关文档:

https://docs.angularjs.org/api/ngResource/service/美元资源

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