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

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

我想获取类别和同一类别的库存, 我可以使用 populate 获取库存及其类别..但我想要它的反转..但我不知道如何..我使用 populate 函数但它不起作用

库存模型 “使用严格”;

/**
 * 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()
};

最佳答案

要以相反的顺序进行填充,您需要在 CategoryInventory 模型之间创建一个“多对多关联”。请参阅以下内容了解如何执行相同操作:

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

关于node.js - 想要在库存和类别模型(mongodb)中的 sails js 中执行联接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38821766/

相关文章:

angularjs - 单独的 REST 后端 API 的优点?

mongodb - 如何将 MongoDB 集合转换为以元素值为键的对象?

npm - 在 SailsJS 中管理依赖关系

javascript - 在 SailsJs 中同时开发 Rest API 和服务器端渲染

javascript - 代码在 chrome 控制台中工作但不在页面中

arrays - 在 MongoDB 中搜索多个字段的多个值

mongodb - 如何将两个数组转换为 mongoDB 中的对象,其中第一个数组具有多个相同的值

javascript - 如何使用 Material UI 在滚动时使 AppBar 和表格的标题粘在同一页面上(因此彼此堆叠)

javascript - Mongodb 更新过期

c++ - 使用 #ifndef 会破坏 node-gyp 模块