【问题标题】:Return boolean (true | false) value when all delete operations are successfully performed成功执行所有删除操作时返回布尔值 (true | false)
【发布时间】:2018-03-24 20:50:32
【问题描述】:

我正在使用 Sequelize、GraphQL 和 Typescript 来编写代码。 我有两个表 RecordInformation 和 OtherDescription。 RecordInformation 在 OtherDescription 表上有一个外键“OtherID”。

我的解析器文件中有以下两个代码。我可以使用 sequelize 中的级联选项删除记录。

但是,我想知道如何根据每个操作是否成功执行来更改此代码以仅返回 true 或 false 值。目前map操作的返回类型为Bluebird[]。我想在删除突变后返回布尔值。

interface IRecordInformation {
    id: number;
    RecordID: number;
    LabelOptionID: number;
    OtherID: number;
}

interface IListRecordInformation {
    RecordInformationList: IRecordInformation[];
}

deleteRecordInformation(_: null, args: IListRecordInformation) {
  const recordInformationList = args.RecordInformationList;
  return recordInformationList.map((recordInfo) => OtherDescription.destroy({
    where: { id: recordInfo.OtherID }
  }));
},

【问题讨论】:

  • 您可以使用.every()OtherDescription.destroy({ where: { id: recordInfo.OtherID } }) 是否返回 Boolean
  • 不,它返回一个数字,即影响的行数
  • 布尔转数字的逻辑是什么?或者您想确定.map() 何时完成?
  • 我尝试添加此代码: deleteRecordInformation(_: null, args: IListRecordInformation) { const recordInformationList = args.RecordInformationList; return recordInformationList.map((recordInfo) => OtherDescription.destroy({ where: { id: recordInfo.OtherID } })).every(number => number > 0); },但它给出了 operator > 不能应用于 Bluebird 的错误
  • OtherDescription.destroy({ where: { id: recordInfo.OtherID } }) 是否返回 Promise

标签: javascript postgresql typescript sequelize.js graphql


【解决方案1】:

您可以使用Promise.all() 传递recordInformationList,然后使用.every() 链接.then() 来检查返回的Promise

Promise.all(
  recordInformationList.map(recordInfo => 
    OtherDescription.destroy({
      where: { id: recordInfo.OtherID }
    })
  )
)
.then(values => values.every(n => n > 0))
.then(bool => bool);

【讨论】:

    猜你喜欢
    • 2012-08-04
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    相关资源
    最近更新 更多