在我的chrome开发人员工具中看不到Restangular响应数据集...是不是我的JSON包含嵌套的JSON?

时间:2013-06-24 12:52:04

标签: angularjs mlab

我目前正在尝试获得一个基本的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']}

通过简单地将course.title,course.course_id等拉入ng-repeat,将其串联到angularJS中时效果非常好。即使是前辈也很容易通过将其构建为直接JSON来管理。

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

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

![Restangular Response]: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;
  });

1 个答案:

答案 0 :(得分:7)

我是Restangular的创造者。

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

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

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

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

贝斯茨!