gpt4 book ai didi

ember.js - 如何使用 store.createRecord() 传递 hasMany 关系的 ID?

转载 作者:行者123 更新时间:2023-12-04 15:40:22 25 4
gpt4 key购买 nike

假设我有以下模型:

// customer.js
DS.Model.extend({
products: DS.hasMany('product')
});

// product.js
DS.Model.extend({
customer: DS.belongsTo('customer')
});

我需要创建一个包含产品列表的客户( 尚未从后端加载 ),大致如下:
this.get('store').createRecord('customer', {products: [1, 2, 3]});  

但这失败了,因为商店希望产品是 DS.Model 数组:

Error while processing route: index Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [1,2,3]



如何使用 ID 提供的关联创建记录?

最佳答案

Error while processing route: index Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [1,2,3] Error



由于错误指出您需要传递 DS.Model 的实例,但您只能使用 createRecord 创建实例,因此您可能需要执行以下操作,
    let product1 = this.store.createRecord('product',{customer:1});
let product2 = this.store.createRecord('product',{customer:2});
return this.get('store').createRecord('customer', {products:[product1,product2]});

关于ember.js - 如何使用 store.createRecord() 传递 hasMany 关系的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42964727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com