我要取同一种类和同一种类的存货,
我可以使用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()
};
最佳答案
要使populate以相反的顺序工作,您需要在"Many to Many Association"
和Category
模型之间创建一个Inventory
。请参阅以下内容以了解如何执行相同操作:
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/