0

I'm building a webapp with node server and nuxt. I'm really stuck when it comes to writing sequelize queries dynamically, adding parameters dynamically. I'm trying to retrieve from my database all the resources which pass some filter criteria (like author name, typology tag.. ). ..

My idea was: Instead of creating a static : `

const result = await Resource.findAll({
        include:[
      {
        model: TypologyTag,
        as: "typologyTag",
        where:{ keyword: { [Op.in]: typologyTag } };
      },
      {
        model: User,
        as: "author",
        where: { name : { [Op.in]: author } }
      }
    ]
});

I was thinking about a more dynamic kind of approach, like:

 var include = [
      {
        model: TypologyTag,
        as: "typologyTag",

      },
      {
        model: User,
        as: "author",
      }
    ];

    if (typologyTag) {
      include[0].['where'] = { keyword: { [Op.in]: typologyTag } };
    };
    if (author) {
      include[1].['where'] = { name: { [Op.in]: author } };
    };

     const result = await Resource.findAll({
        include: include
})


` Because it is highly possible that either the TypologyTag or the author's name are null, so I need to do a check before.

But this doesn't work at all since typescript is giving me an error: TypeError: value.map is not a function at PostgresQueryGenerator ecc

  • If you use `Op.in` then `typologyTag` and `author` should be arrays. Alternatively you can use `Op.eq` or a simplified form `{ name: author }` – Anatoly Nov 19 '22 at 09:17

0 Answers0