【问题标题】:sequelize node express javascript续集节点快递javascript
【发布时间】:2021-12-05 12:20:29
【问题描述】:

大家好,我使用 node-express 和 sequelize 时,每当我使用此查询时,我得到的计数不正确。标签产品在返回计数 3 时包含一个产品。 代码如下

let product = await model.Product.findAndCountAll({
    where:condition, 
    attributes:['id' , 'product_name' , 'description' , 'model_information' , 'brand', 'ProductWarrenty' , 'WarrentyDuration' , [price ,'price']],
    include:[{
      model:model.Category,as:'productcategory'
      
    } , {
      model:model.SubCategory, as:'ProductSubCategory'
    } , {
      model:model.ProductImages,as:'product_images'
    }],
    offset: offset,
      limit: 14,
      order: [sorting]
  })

product 表有 1 条记录,但它返回 3 条记录。

【问题讨论】:

    标签: javascript mysql node.js express sequelize.js


    【解决方案1】:
    yes i also solved it 
    use unique:true 
    Product.findAndCountAll({
            where:condition, 
    distinct:true,
    unique:true,
    })
    

    【讨论】:

      【解决方案2】:
      yeah i solved it.
      use two properties
          unique:true,
          distinct:true,
      use this query 
      
      et product = await model.Product.findAndCountAll({
              where:condition, 
      distinct:true,
      unique:true,
              attributes:['id' , 'product_name' , 'description' , 'model_information' , 'brand', 'ProductWarrenty' , 'WarrentyDuration' , [price ,'price']],
              include:[{
                model:model.Category,as:'productcategory'
                
              } , {
                model:model.SubCategory, as:'ProductSubCategory'
              } , {
                model:model.ProductImages,as:'product_images'
              }],
              offset: offset,
                limit: 14,
                order: [sorting],
            })
      

      【讨论】:

        猜你喜欢
        • 2021-08-15
        • 2020-04-02
        • 2011-08-13
        • 2017-04-03
        • 2016-02-13
        • 2018-04-10
        • 2016-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多