简体   繁体   中英

Waterline Sails JS doesn't work

I've using associations in Sails JS. I've been building an ecommerce app, in which multiple models are associated to one another, but one of associated doesn't work.

In my controller, I'm trying like this:

'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);
        });

    }

And In My model, I'm trying like this 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' }
    }

The PosItem.find({'id':updateCart.item}).populate('productCustomize') doesn't populate in PosItem . If I try to add to cart the 'productcustom' property, like product, it'll show it's 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"
  }
]

Anyone can help me to solve this to be result at the bottom? :)

[
  {
    "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"
  }
]

You have so many fields! :-)

My suggestion to find the problem would be to start backwards and then get to where you want.

Go to https://github.com/balderdashy/waterline-docs/blob/master/models/associations/associations.md and see if you can make the basic association model work.

Then step by step start adding your complexity. If you follow this gradual approach eventually you will stumble upon where the problem lies.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM