我是Ember的新手,并且在JavaScript方面不是很强大。
我正在尝试在后端使用Java Spring MVC,并在前端使用Ember。当我尝试访问页面时。 Ember向服务器发出数据请求。服务器使用数组回答,但是在Ember端没有正确检索数组。我得到一个例外。
Error while loading route: Error: No model was found for '0'
at new Error (native)
at Error.Ember.Error (http://localhost:8080/libs/js/ember.js:844:19)
at Ember.Object.extend.modelFor (http://localhost:8080/libs/js/ember-data.js:9805:33)
at JSONSerializer.extend.extractArray (http://localhost:8080/libs/js/ember-data.js:3172:28)
at superWrapper (http://localhost:8080/libs/js/ember.js:1239:16)
at Ember.Object.extend.extractFindAll (http://localhost:8080/libs/js/ember-data.js:2380:21)
at Ember.Object.extend.extract (http://localhost:8080/libs/js/ember-data.js:2365:37)
at http://localhost:8080/libs/js/ember-data.js:10396:34
at invokeCallback (http://localhost:8080/libs/js/ember.js:9753:19)
at publish (http://localhost:8080/libs/js/ember.js:9423:9)
当调试javascript时,我发现有一个循环遍历数组
for (var prop in payload)
for for循环不是检索数组元素,而是抛出属性。这可能是因为他们在this question on SO中谈论的内容。
对我来说 - 有问题的代码在github here上。
我做错了吗?我应该报告错误吗?
我创建了以下Ember模型:
App.User = DS.Model.extend({
email: DS.attr('string'),
active: DS.attr('boolean'),
});
这就是服务器端的映射方式
@RequestMapping("/users")
public
@ResponseBody
List<User> users() {
System.out.println("getting users");
List<User> list = new LinkedList<User>();
list.add(new User(1,"test@b.c"));
list.add(new User(2,"test2@b.c"));
list.add(new User(3,"test3@b.c"));
return list;
}
这是来自小提琴手的JSON:
[{"id":1,"email":"test@b.c","active":true},{"id":2,"email":"test2@b.c","active":false},{"id":3,"email":"test3@b.c","active":false}]
答案 0 :(得分:2)
默认情况下,我相信ember-data希望你的JSON响应以模型名称为前缀,即使它是一个数组。这意味着您的数据需要格式化为:
{
"users":[
{
"id":1,
"email":"test@b.c",
"active":true
},
{
"id":2,
"email":"test2@b.c",
"active":false
},
{
"id":3,
"email":"test3@b.c",
"active":false
}
]
}