javascript - Waterline Sails JS 不起作用

标签 javascript mongodb associations sails.js waterline

我在 Sails JS 中使用了关联。我一直在构建一个电子商务应用程序,其中多个模型相互关联,但其中一个关联不起作用。

在我的 Controller 中,我尝试这样:

'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"
  }
]

最佳答案

你的领域真多! :-)

我对发现问题的建议是向后开始,然后到达你想要的位置。

转到https://github.com/balderdashy/waterline-docs/blob/master/models/associations/associations.md看看你是否能让基本的关联模型发挥作用。

然后逐步开始增加你的复杂性。如果您遵循这种渐进的方法,最终您会偶然发现问题所在。

祝你好运!

关于javascript - Waterline Sails JS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475316/

相关文章:

javascript - Uncaught ReferenceError 。在 jQuery 事件处理程序中找不到原型(prototype)函数

mongodb - 碎片 : How to write items of different spiders to different MongoDB collections?

ruby-on-rails - ActiveRecord - 嵌套包含

ruby-on-rails-3 - 轨道 3 : Has_many through and Me

javascript - 将 Python 字符串与 Javascript 值结合起来

javascript - 在 MVC Razor 的 Javascript 中,用模型中的值替换关键字

mongodb - 无法连接到 Mlab

grails - grails:获取多个hasmany列表

javascript - 关于javascript字符串替换方法的问题

c# - 如何在 C# Mongodb 强类型驱动程序中基于嵌套数组元素进行过滤