0

我已经在这几天了,似乎无法让它发挥作用。我的问题是我在前端使用 Restler(版本 3)作为 API 和 Restangular,我收到以下错误:

Error: can't convert undefined to object restangularizeBase@http://localhost/vendor/restangular/src/restangular.js:436 restangularizeCollection@http://localhost/vendor/restangular/src/restangular.js:552 createServiceForConfiguration/fetchFunction/<@http://localhost/vendor/restangular/src/restangular.js:610 Qc/e/j.promise.then/h@http://localhost/vendor/angular/angular.min.js:78 Qc/g/<.then/<@http://localhost/vendor/angular/angular.min.js:78 e.prototype.$eval@http://localhost/vendor/angular/angular.min.js:88 e.prototype.$digest@http://localhost/vendor/angular/angular.min.js:86 e.prototype.$apply@http://localhost/vendor/angular/angular.min.js:88 e@http://localhost/vendor/angular/angular.min.js:95 p@http://localhost/vendor/angular/angular.min.js:98 Yc/</t.onreadystatechange@http://localhost/vendor/angular/angular.min.js:99

以下是相关的代码片段,您可以看到我在做什么:

Restler 设置来设置我的 API

$r = new Restler();
$r->addAPIClass('User');
$r->handle();

我将为我的 API 访问的用户类对象(现在我只是返回一个示例进行测试)

class User{
    public function index() {
        return  array(
            array(
                'first_name'=>'John',
                'last_name'=>'Smith',
                'role'=>'supervisor',
            ),
            array(
                'first_name'=>'Matt',
                'last_name'=>'Doe',
                'role'=>'employee',
            ),

        );
    }
}

最后是我的 app.js 文件

'use strict';

var app = angular.module('cma',['restangular']);

app.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('/api');
    RestangularProvider.setExtraFields(['name']);
    RestangularProvider.setResponseExtractor(function(response,operation) {
        return response.data;
    });
});

app.run(['$rootScope','Restangular',function($rootScope,Restangular) {
    var userResource = Restangular.all('session');
    $scope.test = userResource.getList(); // This is where the error is happening 
}]);

API 正在返回以下 JSON 响应(取自 Firebug:

GET http://localhost/api/user 200 OK 96ms

):

[
    {
        "first_name": "John",
        "last_name": "Smith",
        "role": "supervisor"
    },
    {
        "first_name": "Matt",
        "last_name": "Doe",
        "role": "employee"
    }
]

我看不到正在发生的任何会导致问题的事情。任何帮助将不胜感激!

4

1 回答 1

0

我是 Restangular 的创造者 :)

问题是您正在使用 responseInterceptor 并实际返回一个数组。

因此,您的服务器正在返回一个数组。你的 responseInterceptor 得到它,然后返回data你的数组的变量。由于这是未定义的,因此未定义将发送到 Restangular,因此您会收到该错误。

删除 responseInterceptor,一切都将开始工作:)。

最好的

于 2013-08-01T21:30:00.380 回答