【问题标题】:Sequelize raw query, get all data or specific data with bind (postgresql)Sequelize原始查询,使用绑定(postgresql)获取所有数据或特定数据
【发布时间】:2021-12-13 21:16:58
【问题描述】:

所以当我使用原始查询选择时我有产品表,如果未指定集合,那么它将获取所有数据,但如果指定了集合,它将获取每个集合。这是我的代码:

  const { category, collection } = req.query;
  const sql = `
    select 
     p.id,
     p.p_image, 
     p.p_name, 
     p.p_desc,
     p.p_prize, 
     p.p_stock,
     c.c_name,
     cl.cl_name
    from products as p
    inner join collections as cl on cl.id = p.p_collection_id 
    inner join categories as c on c.id = cl.cl_category_id
    where (c.c_name = $1 and cl.id = $2) or (c.c_name = $1)
    order by p."createdAt" desc; `;

   try {
      const getData = await Product.sequelize.query(sql, {
      bind: [category, collection],
   });
   
   if (getData[0] != "") {
       res.status(200).send({
          s: 1,
          message: "success retrive all products",
          data: getData[0],
       });
    } else {
       res.status(404).send({
          s: 0,
          message: "data not found",
       });
     }
  } catch (err) {
    res.status(500).send({
      message: err,
   });
  }
  };

我想要实现的是:

  1. http://localhost:3001/api/v1/product/get?category=man 时,我将获得每个类别的所有数据man, woman, or kid
  2. http://localhost:3001/api/v1/product/get?category=man&collection=1 时,我将获取每个类别的所有数据man, woman or kid,但每个ID 的特定集合1=topwear, 2=bottomwear 等。

当我在上面做的时候它真的做了什么:

  1. 只有在http://localhost:3001/api/v1/product/get?category=man时才会得到message: {}
  2. http://localhost:3001/api/v1/product/get?category=man&collection=1时获取所有数据,不关心集合

我希望你能理解我的意思并能帮助我......

【问题讨论】:

    标签: node.js postgresql express sequelize.js


    【解决方案1】:

    我想你需要添加一个条件来检查通过collection 值:

     where (c.c_name = $1 and cl.id = $2 and $2 is not null) or (c.c_name = $1 and $2 is null)
    

    当然,如果url中没有指明对应的查询参数,你需要在collection变量中传递null

    附:在 bind 选项和 SQL 查询本身中使用命名参数会更好,更易读,如下所示:

    const sql = `
        select 
         p.id,
         p.p_image, 
         p.p_name, 
         p.p_desc,
         p.p_prize, 
         p.p_stock,
         c.c_name,
         cl.cl_name
        from products as p
        inner join collections as cl on cl.id = p.p_collection_id 
        inner join categories as c on c.id = cl.cl_category_id
        where (c.c_name = $category and cl.id = $collection and $collection is not null)
           or (c.c_name = $category and $collection is null)
        order by p."createdAt" desc; `;
    
       try {
          const getData = await Product.sequelize.query(sql, {
          bind: { category, collection },
       });
    

    【讨论】:

    • 我尝试了您的解决方案。它适用于http://localhost:3001/api/v1/product/get?category=man&collection=1 我得到了我想要的,但是如果集合为空或者我没有像collection=nullcollection= 那样填充它,我在邮递员中遇到错误说错误代码code: 22P02 parameters: [ "man",""] for ""对于nullparameters: [ "man","null"]
    • 如果collection 查询参数未指示或没有值或有null 字符串值,则只需传递null 即可解决问题
    • http://localhost:3001/api/v1/product/get?category=man&collection=null 你的意思是这样吧?我这样做了,但没有得到想要的结果。我得到了上面提到的错误,因为collection 必须是整数数据类型。我试过http://localhost:3001/api/v1/product/get?category=man 结果是message:{}
    • 你是否像这样通过collection(!collection || collection === 'null' ? null : parseInt(collection)
    • 是的。终于成功了,非常感谢我的朋友:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多