【问题标题】:Passing an Array of Values, using Typescript/Sequelize使用 Typescript/Sequelize 传递值数组
【发布时间】:2020-01-29 21:48:08
【问题描述】:

下午好, 我是一名初级开发人员,一直在尝试使用 Typescript 学习 Sequelize,但遇到了困难。 我创建了一个简单的查询来根据一个值进行搜索,但我无法在不破坏查询的情况下传递一组值。

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

现在,如果我将邮递员的查询传递为

Name: "Joe"
Limit: 10,

它返回了 Joe 的 10 笔交易,但我希望能够在 查询如:

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

我相信答案会以某种方式映射数组,或者使用 Sequelize.Op,但我在尝试时似乎遇到了多个打字稿错误。

如果您能向我解释我做错了什么,作为学习经验将不胜感激。

谢谢!

【问题讨论】:

    标签: javascript sql node.js typescript sequelize.js


    【解决方案1】:

    您必须使用 sequelize 中的 or 关键字,例如提及 in the documentation

    例子:

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

    如果您愿意,也可以使用in

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

    【讨论】:

    • 谢谢!我不得不将 [Op.or]: Names 修改为在它自己的对象内,但效果很好!
    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多