0

Good afternoon, I'm a junior dev who's been trying to learn Sequelize with Typescript but am having difficulties. I've created a simple query to search based off a value, but am unable to pass an array of values without breaking the query.

const findNameTsxData = (limit: number, Name: string): Promise<FinanceStuffs[]> => {
// const { in: opIn, or } = Sequelize.Op;
  return db.FinanceStuffs.findAll({
    where: { Name },
    limit
  });
};

Right now, if i pass a query on postman as

Name: "Joe"
Limit: 10,

It returns me 10 transactions by Joe, but i'd like to be able to pass multiple names in the query such as:

Name: ["Joe", "Alex", "Jacob"]
Limit: 30

I believe the answer would be mapping the array somehow, or using Sequelize.Op but i seem to be getting multiple typescript errors when tried.

If you could explain to me what i'm doing wrong, it would be greatly appreciated as a learning experience.

Thank you!

1 Answer 1

2

You have to use the or keyword from sequelize like mention in the documentation

Example:

const findNameTsxData = (limit: number, Names: string[]): Promise<FinanceStuffs[]> => {
const Op = Sequelize.Op;
  return db.FinanceStuffs.findAll({
    where: { Name: { [Op.or]: Names } },
    limit
  });
};

You can also use the in if you prefer

const findNameTsxData = (limit: number, Names: string[]): Promise<FinanceStuffs[]> => {
const Op = Sequelize.Op;
  return db.FinanceStuffs.findAll({
    where: { Name: { [Op.in]: Names } },
    limit
  });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I had to modify the [Op.or]: Names to be inside of its own object, but it worked great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.