Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 什么是「;“正确”;实现“a”的途径;“平坦”;使用sequelize N:M关联的结果_Node.js_Orm_Sequelize.js - Fatal编程技术网

Node.js 什么是「;“正确”;实现“a”的途径;“平坦”;使用sequelize N:M关联的结果

Node.js 什么是「;“正确”;实现“a”的途径;“平坦”;使用sequelize N:M关联的结果,node.js,orm,sequelize.js,Node.js,Orm,Sequelize.js,我有一个产品数据库,其中包含一些1-n个类别的产品。我的产品存储在products表中,类别存储在categories中,关联存储在productCategoryRel中。现在,我的愿望是,当我使用product.find()获得一个产品时,接收一个包含categories属性的结果,其中包含categories表中的数据,而不包含任何其他嵌套属性 目前我得到的是: { "id": 1, "title": "Testproduct", "categories": [ {

我有一个产品数据库,其中包含一些1-n个类别的产品。我的产品存储在
products
表中,类别存储在
categories
中,关联存储在
productCategoryRel
中。现在,我的愿望是,当我使用
product.find()
获得一个产品时,接收一个包含
categories
属性的结果,其中包含
categories
表中的数据,而不包含任何其他嵌套属性

目前我得到的是:

{
  "id": 1,
  "title": "Testproduct",
  "categories": [
    {
      "id": 1,
      "title": "Testcategory",
      "productCategoryRel": {
        "category_id": 1,
        "product_id": 1
      }
    }
  ]
}
因此,我的
product
包含一个属性
categories
,这很好,但是
categories
还包含
productCategoryRel
表中我想要避免的所有数据。我已经用谷歌搜索过了,研究过文档等等,我甚至不确定我想实现的目标是否可能

这就是我定义(测试)模型的方式:

各协会:

Product.belongsToMany(Category, {
    through: ProductCategoryRel,
    foreignKey: 'product_id',
});

Category.belongsToMany(Product, {
    through: ProductCategoryRel,
    foreignKey: 'category_id',
});

ProductCategoryRel.hasMany(Product, {foreignKey: 'id'});
ProductCategoryRel.hasMany(Category, {foreignKey: 'id'});
我想要的结果非常简单:

{
  "id": 1,
  "title": "Testproduct",
  "categories": [
    {
      "id": 1,
      "title": "Testcategory"
    }
  ]
}
那么这可能吗?我需要如何更改我的关联?我已经尝试了一些奇怪的hasMany/belongsTo组合,但是没有成功

我正在使用Sequelize 3.12.1。

尝试类似的方法(可能有一些错误,但如果您给我反馈,我们可以解决问题):

属性
属性允许您选择要显示的数据。也可在随附型号中使用


文档中的更多内容

我希望尽可能避免这种情况,因为我必须明确排除每个范围中的每个属性(即使是新属性,以防我向模型中添加一些属性),这听起来非常冗余/开销//编辑:顺便说一下:属性似乎只适用于包含模型中指定的列(在我的例子中是类别),而不适用于关系。我仍然看到
productCategoryRel
我已经更新了我的代码片段。我通过{attributes:[]}添加了
,应该从输出中删除导致错误的
ProductCategoryRel
:W:\temp\node\u modules\mysql\lib\protocol\Parser.js:82 throw err;^TypeError:无法设置未定义的属性“productCategoryRel”
非常奇怪。如果不使用此代码,我将无能为力。我现在留下我的答案,以便其他人有一个关于我们已经检查过的内容的记录,并且可以用新的命题添加答案。啊,你是对的。我使用了
attributes[]
而不是
通过:{attributes:[]}
-非常感谢!
{
  "id": 1,
  "title": "Testproduct",
  "categories": [
    {
      "id": 1,
      "title": "Testcategory"
    }
  ]
}
Product.findOne({
    where: {
      id: yourProductId
    },
    include: [{
      model : Category,
      order: [['id','desc']],
      through: {attributes: []}
    }],
  })