我在Sails JS中使用了关联。我一直在构建一个电子商务应用程序,其中多个模型彼此关联,但其中一个模型不起作用。
在我的控制器中,我正在尝试这样:
'synchronize' : function(req,res,next){
var owner = req.param('owner'),
store = req.param('store'),
items = req.param('items'),
localCart = {
'items' : items,
'owner' : owner,
'store' : store
},
updatedCart = {};
// get cart first
PosCart.findOrCreate({
'owner' : owner,
'store' : store
},localCart)
.populate('items')
.then(function(serverCart){
if(!serverCart) throw 'Kesalahan Sistem';
// update server cart
_.each(localCart.items, function(item,index){
var _server_index = {};
// check if product exist
_server_index =_.findIndex(serverCart.items, function(_item){
return _item.product === item.product.id &&
_item.productCustomize === item.productCustomize.id &&
_.isEqual(_item.attributes,item.attributes)
});
// product already exist but have different quantity
if(_server_index > -1){
item.quantity = (item.quantity > serverCart.items[_server_index].quantity)?
item.quantity : serverCart.items[_server_index].quantity ;
serverCart.items[_server_index] = _.clone(item);
}else{
// this is new items
serverCart.items.push(_.clone(item));
}
});
// update cart then
return PosCart.update(serverCart.id, serverCart);
})
.then(function(carts){
if(!carts) throw 'Kesalahan Sistem';
// return new updated cart
// 2 level populate
updatedCart = _.clone(carts[0]);
return [
PosCart.findOne(updatedCart.id).populate('items'),
PosItem.find({'id':updatedCart.item}).populate('product'),
PosItem.find({'id':updateCart.item}).populate('productCustomize')
];
})
.spread(function(cart,items){
if(!cart) throw 'kesalahan sistem'
_.each(cart.items,function(item,index){
cart.items[index] = _.find(items,function(_item){
return _item.id === item.id;
});
});
return res.json(cart);
})
.catch(function(error){
next(error);
});
}
而在我的模型中,我正在尝试这样做 的 PosCart.js
attributes: {
items : { collection : 'PosItem', via : 'cart' },
// owner
owner : { model : 'CrmCustomer' },
// store referrer
store : { model : 'SystemStore' }
}
PosItem.js
attributes: {
product : { model : 'PosProduct' },
productCustomize : { model : 'PosCustomProduct' },
variant : { model : 'PosProductVariant' },
attributes : { type : 'json', defaultsTo : {} },
quantity : { type : 'integer', defaultsTo : 1 },
// owner
cart : { model : 'PosCart' },
wishlist : { model : 'PosWishlist' }
}
PosItem.find({'id':updateCart.item}).populate('productCustomize')
未填充 PosItem 。如果我尝试添加“productcustom”属性,就像产品一样,它会显示它的ID。
[
{
"product": {
"display": "5636fe51effd3d4508d16cc8",
"materials": [],
"store": "5636fd43effd3d4508d16cb5",
"name": "Penny",
"basePrice": 250000,
"category": "5636fe14effd3d4508d16cc7",
"attributes": {
"Bahan": [
"Kulit"
],
"Ukuran": [
"38"
]
},
"desc": "Lorem ipsum dolor sit amet",
"published": true,
"createdAt": "2015-11-02T06:10:25.296Z",
"updatedAt": "2015-11-02T06:10:25.395Z",
"id": "5636fe51effd3d4508d16cc9"
},
"variant": {
"name": "5636fe51effd3d4508d16cc9-Bahan:Kulit-Ukuran:38",
"Bahan": "Kulit",
"Ukuran": "38",
"product": "5636fe51effd3d4508d16cc9",
"additionalPrice": 0,
"createdAt": "2015-11-02T06:10:25.508Z",
"updatedAt": "2015-11-02T06:10:25.508Z",
"id": "5636fe51effd3d4508d16cca"
},
"cart": {
"store": "5632e638954e0b843f285faa",
"createdAt": "2015-11-02T06:13:28.708Z",
"updatedAt": "2015-11-02T06:13:28.738Z",
"id": "5636ff08effd3d4508d16cce"
},
"quantity": 1,
"attributes": {
"Bahan": "Kulit",
"Ukuran": "38"
},
"createdAt": "2015-11-02T06:13:28.757Z",
"updatedAt": "2015-11-02T06:13:28.757Z",
"id": "5636ff08effd3d4508d16cd0"
},
{
"variant": {
"name": "5637016deffd3d4508d16cdc-Bahan:Kulit-Soles:Outsole-Ukuran:38",
"Bahan": "Kulit",
"Soles": "Outsole",
"Ukuran": "38",
"product": "5637016deffd3d4508d16cdc",
"additionalPrice": 25000,
"createdAt": "2015-11-02T06:23:41.862Z",
"updatedAt": "2015-11-02T06:24:53.995Z",
"display": [
{
"zoom": "file/65e1d275-c3a7-4502-a0fe-6f5ac299d00d.jpg",
"gallery": "file/ecbf0705-88ce-41dc-8ba2-4755041b623e.jpg",
"thumbnail": "file/0db0e9e8-9294-4df0-b173-7d6de822786a.jpg",
"active": true
}
],
"id": "5637016deffd3d4508d16cdd"
},
"cart": {
"store": "5636fd43effd3d4508d16cb5",
"owner": "56370d92509c2c470a3d33ac",
"createdAt": "2015-11-02T07:15:47.026Z",
"updatedAt": "2015-11-02T07:16:11.749Z",
"id": "56370da3509c2c470a3d33af"
},
"quantity": 1,
"attributes": {
"Bahan": "Kulit",
"Soles": "Outsole",
"Ukuran": "38"
},
"createdAt": "2015-11-02T07:16:11.809Z",
"updatedAt": "2015-11-02T07:16:11.809Z",
"id": "56370dbb509c2c470a3d33b1"
}
]
任何人都可以帮助我在底部解决这个问题吗? :)
[
{
"product": {
"display": "5636fe51effd3d4508d16cc8",
"materials": [],
"store": "5636fd43effd3d4508d16cb5",
"name": "Penny",
"basePrice": 250000,
"category": "5636fe14effd3d4508d16cc7",
"attributes": {
"Bahan": [
"Kulit"
],
"Ukuran": [
"38"
]
},
"desc": "Lorem ipsum dolor sit amet",
"published": true,
"createdAt": "2015-11-02T06:10:25.296Z",
"updatedAt": "2015-11-02T06:10:25.395Z",
"id": "5636fe51effd3d4508d16cc9"
},
"productCustomize": {
"display": "5636fe51effd3d4508d16dd9",
"materials": [],
"store": "5636fd43effd3d4508d16cb5",
"name": "Beefroll",
"basePrice": 250000,
"category": "5636fe14effd3d4508d16cc7",
"attributes": {
"Bahan": [
"Kulit"
],
"Ukuran": [
"38"
]
},
"variant": {
"name": "5636fe51effd3d4508d16cc9-Bahan:Kulit-Ukuran:38",
"Bahan": "Kulit",
"Ukuran": "38",
"product": "5636fe51effd3d4508d16cc9",
"additionalPrice": 0,
"createdAt": "2015-11-02T06:10:25.508Z",
"updatedAt": "2015-11-02T06:10:25.508Z",
"id": "5636fe51effd3d4508d16cca"
},
"cart": {
"store": "5632e638954e0b843f285faa",
"createdAt": "2015-11-02T06:13:28.708Z",
"updatedAt": "2015-11-02T06:13:28.738Z",
"id": "5636ff08effd3d4508d16cce"
},
"quantity": 1,
"attributes": {
"Bahan": "Kulit",
"Ukuran": "38"
},
"createdAt": "2015-11-02T06:13:28.757Z",
"updatedAt": "2015-11-02T06:13:28.757Z",
"id": "5636ff08effd3d4508d16cd0"
},
{
"variant": {
"name": "5637016deffd3d4508d16cdc-Bahan:Kulit-Soles:Outsole-Ukuran:38",
"Bahan": "Kulit",
"Soles": "Outsole",
"Ukuran": "38",
"product": "5637016deffd3d4508d16cdc",
"additionalPrice": 25000,
"createdAt": "2015-11-02T06:23:41.862Z",
"updatedAt": "2015-11-02T06:24:53.995Z",
"display": [
{
"zoom": "file/65e1d275-c3a7-4502-a0fe-6f5ac299d00d.jpg",
"gallery": "file/ecbf0705-88ce-41dc-8ba2-4755041b623e.jpg",
"thumbnail": "file/0db0e9e8-9294-4df0-b173-7d6de822786a.jpg",
"active": true
}
],
"id": "5637016deffd3d4508d16cdd"
},
"cart": {
"store": "5636fd43effd3d4508d16cb5",
"owner": "56370d92509c2c470a3d33ac",
"createdAt": "2015-11-02T07:15:47.026Z",
"updatedAt": "2015-11-02T07:16:11.749Z",
"id": "56370da3509c2c470a3d33af"
},
"quantity": 1,
"attributes": {
"Bahan": "Kulit",
"Soles": "Outsole",
"Ukuran": "38"
},
"createdAt": "2015-11-02T07:16:11.809Z",
"updatedAt": "2015-11-02T07:16:11.809Z",
"id": "56370dbb509c2c470a3d33b1"
}
]
答案 0 :(得分:1)
你有这么多领域! : - )
我建议找到问题的方法是向后开始,然后到达你想要的地方。
转到https://github.com/balderdashy/waterline-docs/blob/master/models/associations/associations.md,看看是否可以使基本关联模型有效。
然后一步一步开始添加你的复杂性。如果你最终采用这种渐进的方法,你会发现问题所在。
祝你好运!