想要在库存和类别模型(mongodb)中的sails js中执行连接查询

时间:2016-08-08 05:20:10

标签: node.js mongodb sails.js sails-mongo

我想获取相同类别的类别和库存, 我可以通过使用填充来获取库存及其类别..但我想要反转它..但我不知道如何..我使用填充功能但它不工作

库存模型     "使用严格的&#34 ;;

/**
 * Inventory
 * @description :: Model for storing Inventory records
 */

module.exports = {
  schema: true,
  autoPk:false,
  attributes: {
    // Fill your attributes here

    id: {
      type: 'string',
      primaryKey: true
    },
    name: {
      type: 'string'
    },
    brand: {
      type: 'string'
    },
    price: {
      type: 'float'
    },
    discount: {
      type: 'string'
    },
    description: {
      type: 'string'
    },
    business: {
      model: 'Business'
    },
    images: {
      type: 'array'
    },
    quantity: {
      type: 'integer',
      defaultsTo: 1
    },
    inStock: {
      type: 'boolean',
      defaultsTo: true
    },
    category: {
      model: 'Category'
    },

    categoryName: {
      type: 'string'
    },

    deviceId: {
      type: 'string'
    },

    localId: {
      type: 'string'
    },

    productId: {
      type: 'string'
    },

    costPrice: {
      type: 'float'
    },

    supplierName: {
      type: 'string'
    },

    notes: {
      type: 'array'
    },

    extra: {
      type: 'object'
    },

    reviews: {
      collection: 'Review',
      via: 'inventory'
    },

    views: {
      type: 'integer',
      defaultsTo: 0
    },

    lastSeen: {
      type: 'datetime',
      defaultsTo: null
    },

    toJSON() {
      return this.toObject();
    }
  },

  beforeUpdate: (values, next) => next(),
  beforeCreate: (values, next) => next()
};


and category model is

"use strict";

/**
 * Category
 * @description :: Model for storing Category records
 */

module.exports = {
  schema: true,
  attributes: {
    // Fill your attributes here
    id: {
      primaryKey: true,
      type: 'string'
    },
    name: {
      type: 'string'
    },
    business: {
      model: 'Business'
    },

    deviceId: {
      type: 'string'
    },

    localId: {
      type: 'string'
    },
    toJSON() {
      return this.toObject();
    }
  },

  beforeUpdate: (values, next) => next(),`enter code here`
  beforeCreate: (values, next) => next()
};

1 个答案:

答案 0 :(得分:2)

要使填充按相反顺序工作,您需要在 Start 11/12/2001 12/01/2014 23/12/2001 "Many to Many Association"模型之间创建Category。请参阅以下内容以了解如何执行相同的操作:

https://github.com/balderdashy/waterline-docs/blob/master/models/associations/many-to-many.md