Javascript 余烬只设置一个元素,而不是多个元素

Javascript 余烬只设置一个元素,而不是多个元素,javascript,ember.js,ember-data,Javascript,Ember.js,Ember Data,我有两个异步模型: App.Posts = DS.Model.extend({ 'content': attr('string'), 'comments': DS.hasMany('comments', {async: true}), }); App.Comments = DS.Model.extend({ 'body': DS.attr('string'), 'postId': DS.belongsTo('posts', {async: true}) });

我有两个异步模型:

App.Posts = DS.Model.extend({
    'content': attr('string'),
    'comments': DS.hasMany('comments', {async: true}),
});

App.Comments = DS.Model.extend({
    'body': DS.attr('string'),
    'postId': DS.belongsTo('posts', {async: true})
});
通过PostController,我尝试通过一个操作在单击时加载注释:

App.PostController = Ember.ArrayController.extend({
    loadComments: function(post_id) {
        this.store.find('comments', post_id);
    }
});
(也许有更好的方法可以做到这一点??)

请求和API响应是正确的(请参见下面的API响应),但仅呈现一条注释,然后Ember抛出错误:

TypeError: Cannot read property 'postId' of undefined
在Embers Console>Data选项卡中,注释模型中有一条注释,但注释模型中还有一个post元素,其注释属性设置为undefined。这就解释了为什么Ember不能读取postId属性,因为它不是一个注释。为什么余烬将帖子推到评论模型中,并且只将一条而不是三条评论推到模型中

API响应

{
    "comments": [
        {
            "id": 2,
            "postId": 31152,
            "body": "Lorem ipsum dolor sit amet, consetetur",
        },
        {
            "id": 2,
            "postId": 31152,
            "body": "asdasd",
        },
        {
            "id": 2,
            "postId": 31152,
            "body": "asd asd sd",
        }
    ]
}

这是一个在黑暗中拍摄的小镜头,我通常把它作为一个评论,但它有点大。您可以尝试将所有模型引用更改为singular吗。这是余烬数据模型的正确模式

App.Post = DS.Model.extend({
    'content': attr('string'),
    'comments': DS.hasMany('comment', {async: true}),
});

App.Comment = DS.Model.extend({
    'body': DS.attr('string'),
    'postId': DS.belongsTo('post', {async: true})
});

this.store.find('comment', post_id);

现在我写这篇文章,我可能会看到另一个问题。如果您是通过post_id查询注释(假设它是
7
),那么余烬数据期望返回一条记录,而不是一组记录。因此,它可能会查看评论集合并认为它是一条记录,这就破坏了它的逻辑。

是的,最终Moels的单数是线索,但也可以查询评论:this.store.find('comment',{postId:post_id});因为对于一个普通参数,Ember需要一个对象作为响应。通过查询({postId:post_id}),余烬正在等待一个数组。你能显示你的路由器吗。如果注释嵌套在post资源中,则必须在控制器中指定该注释。