Sequelize 返回结果与 console.log(result) 不同

问题描述

我正在尝试将记录到控制台的完整结果对象通过 res.send(result) 或 res.json(result) 发送给 Postman

我的代码

function updateTaskStatus(taskStatus,EmployerId,batchNum){
    taskStatus == "ACTIVE" ? taskStatus = "COMPLETE" : taskStatus = "ACTIVE";
  console.log(taskStatus)
  return models.changes_data.findOne({
    where: {
      Employer_ID: EmployerId,Batch_Number: batchNum
    },options:{raw:false}
  })
    .then(async (result) => {
      // console.log(result)
      if (result) {
        await result.update({
          Status: taskStatus
        })
          .then((result) => {
            return result;
          })
          .catch((err) => {
            throw err
          })
      }
      console.log(result)
      return result
    })
    .catch((err) => {
      throw err;
    })

console.log 输出

changes_data {
  dataValues: {
    idChanges_Data: REDACTED,Create_Date: REDACTED,Update_Date: REDACTED,Status: 'REDACTED',Batch_Number: REDACTED,Employer_ID: REDACTED
    Record_Type: 'DREDACTED,Plan_ID: REDACTED,EE_LastName: REDACTED,EE_FirstName: REDACTED,EE_TaxID: REDACTED,Change_Effective_Date: REDACTED,End_All_Deferrals: REDACTED,Contribution_Code: REDACTED,Deferral_Percent: REDACTED,Deferral_Amt: REDACTED,Plan_Entry_Date: REDACTED,Loan_Number: REDACTED,Loan_Pmt_Amt: REDACTED,Loan_Goal_Amt: REDACTED,Changes_Date: REDACTED
  },_prevIoUsDataValues: {
    idChanges_Data: REDACTED,Employer_ID: REDACTED
    Record_Type: 'REDACTED',_changed: Set {},_options: {
    isNewRecord: false,_schema: null,_schemaDelimiter: '',raw: true,attributes: [
      'idChanges_Data','Create_Date','Update_Date','Status','Batch_Number','Employer_ID','Record_Type','Plan_ID','EE_LastName','EE_FirstName','EE_TaxID','Change_Effective_Date','End_All_Deferrals','Contribution_Code','Deferral_Percent','Deferral_Amt','Plan_Entry_Date','Loan_Number','Loan_Pmt_Amt','Loan_Goal_Amt','Changes_Date'
    ]
  },isNewRecord: false
}

发送给邮递员身体的内容

{
    idChanges_Data: REDACTED,// note: the task status is successfully being updated.
    Batch_Number: REDACTED,Changes_Date: REDACTED
  }

我很确定这与将 raw 设置为 true 有关,但我不确定需要更改的位置。

_options: {
    isNewRecord: false,

我正在取回更新的数据,所以拥有所有数据并不重要,我只是好奇为什么 console.log 会显示比返回更多的信息,我想知道如何更改它。 非常感谢您的时间和考虑。

解决方法

您通过调用 console.log(result) 看到的是一个 Sequelize 模型实例,如果您将它传递给 res.json,它将被序列化为带有来自 dataValues 的模型道具的普通对象。

如果您希望在 console.log 中看到相同的序列化结果,请使用模型实例的 get 方法,如下所示:

console.log(result.get({ plain: true }))