1

我目前正在尝试建立一个基本的 RESTful 服务,该服务从 MongoLab 中提取我的 JSON 对象,而不是我在沙箱中工作的基于文件的实现。我的每个 JSON 对象都包含一个子数组(它们的前身 ID):

{title: 'xxyy1', course_id: '10004', semester_hours: 3, detail: 'coursename1', predecessors: ['10001']},
{title: 'xxyy2', course_id: '10005', semester_hours: 3, detail: 'coursename2', predecessors: ['10001','10003','10004']},
{title: 'xxyy3', course_id: '10006', semester_hours: 3, detail: 'coursename3', predecessors: ['10001','10003','10004']},
{title: 'xxyy4', course_id: '10007', semester_hours: 3, detail: 'coursename4', predecessors: ['10004','10006']}

这在将其串入 angularJS 时非常有效,只需将 course.title、course.course_id 等拉入 ng-repeat 即可。即使是前辈也可以通过将其构建为直接 JSON 来轻松管理。

<span class="predecessors">{{course.predecessors | json}}</span>

但是,当尝试使用 Restangular 作为我的资源提供者从 MongoLab 提取相同的数据集时,我无法在视图中看到任何响应的数据......实际上,我在任何地方都看不到任何数据。使用 Chrome 的开发工具,我看到的只有以下内容:

![Restangular 响应]:http: //imgur.com/w8qE96K.jpg

我在这里错过了什么吗?我的 Mongo 实现如下:

  RestangularProvider.setRestangularFields({
    id: 'course_id'
  });


  RestangularProvider.setListTypeIsArray(false);

  RestangularProvider.setResponseExtractor(function(response, operation, what, url) {

    var newResponse;
    if (operation === "getList" && what === "availableCourses") {

        console.log(response);

        newResponse = response.data.title;
        newResponse.course_id = response.data.course_id;
        newResponse.semester_hours = response.data.semester_hours;
        newResponse.detail = response.data.detail;
        newResponse.predecessors = response.data.predecessors;
    } else {
        newResponse = response.data;
    }
    return newResponse;
  });
4

1 回答 1

7

我是 Restangular 的创造者。

因此,当您执行 getList 时,Restangular 期望得到一个数组作为响应。在您的情况下,当您获得一个列表并且路径是 availableCourses 时,您将返回一个具有属性的字符串(标题)。所以你正在创建一个带有属性的“特殊”字符串,它并不常见,也不与 Restangular 兼容。

如果要获取所有可用课程的列表,则需要返回一个数组作为新响应。那么,当您执行 getList 时,服务器的确切响应是什么?如果您给我这个,我将能够帮助您更轻松地创建此响应提取器。

也看看这个 MongoLab 例子:),也许它会帮助你。

http://plnkr.co/edit/d6yDka?p=preview

最好的!

于 2013-06-24T22:52:44.643 回答