得到sequelize的响应后修改JSON

时间:2019-02-14 15:17:56

标签: mysql arrays node.js json sequelize.js

我从我的续集函数中得到一个JSON。我必须对此进行修改,因为我必须将其发送给数据库进口商,后者需要以固定形式使用它。 有没有一种方法可以自定义此功能,以便我得到所需的结果?

型号:

User.associate = function (models) {
    User.hasMany(models.SurveyResult)
  }`

    SurveyResult.associate = function (models) {
    SurveyResult.belongsTo(models.User)

功能:

  async mediImport (req, res) {
    try {
      const transaction = await User.findAll({

        where: { released: true },
        // Select forename as Vorname, name as Nachname
        attributes: [
          ['forename', 'PAPPS286'],
          ['name', 'Nachname'],
          ['birthdate', 'PADPS60']
        ],

        include: [{ model: SurveyResult, attributes: ['result'] }]

      }).map(user => user.toJSON())

      res.send({
        transaction

      }
      )
    }

这是我从函数中获得的JSON

{
    "transaction": [
        {
            "PAPPS286": "Tes",
            "Nachname": "Josef",
            "PADPS60": null,
            "SurveyResults": [ {
                    "result": {
                        "name": "blau",
                        "email": "mail",
                        "birthdate": "01.02.1990"
                    }
                }]
        },
        {
            "PAPPS286": "Dampf",
            "Nachname": "Hans",
            "PADPS60": null,
            "SurveyResults": [
                {
                    "result": {
                        "name": "blau",
                        "email": "mail",
                        "birthdate": "01.02.1990"
                    }
                }
            ]
        },    
      ]
    },

这是我需要的JSON表格:

{
        "transaction": [
            PAD{
                "PAPPS286": "Tes",
                "Nachname": "Josef",
                "PADPS60": null,
                "MH":  {
                            "name": "blau",
                            "email": "mail",
                            "birthdate": "01.02.1990"

            },
            PAD{
                "PAPPS286": "Dampf",
                "Nachname": "Hans",
                "PADPS60": null,
                "MH":  {
                            "name": "blau",
                            "email": "mail",
                            "birthdate": "01.02.1990"
                        }

            },    
          ]
        },

也许我回来后可以修改JSON。但是我不知道这怎么工作。

1 个答案:

答案 0 :(得分:1)

下面的map会满足我的需求。您可能需要根据密钥修改它,尤其是在访问所需变量时。 (t.PAPPS286t.PADPS60

此外,我仅获取第一个调查结果SurveyResults。如果不存在或大于1,请不确定要什么。

var obj = {
  "transaction": [{
      "PAPPS286": "Tes",
      "Nachname": "Josef",
      "PADPS60": null,
      "SurveyResults": [{
        "result": {
          "name": "blau",
          "email": "mail",
          "birthdate": "01.02.1990"
        }
      }]
    },
    {
      "PAPPS286": "Dampf",
      "Nachname": "Hans",
      "PADPS60": null,
      "SurveyResults": [{
        "result": {
          "name": "blau",
          "email": "mail",
          "birthdate": "01.02.1990"
        }
      }]
    },
  ]
}


obj.transaction = obj.transaction.map((t) => {
  return Object.assign({
    "PAPPS286": t.PAPPS286,
    "Nachname": t.Nachname,
    "PADPS60": t.PADPS60,
    "MH": {
      "name": t.SurveyResults[0].result.name,
      "email": t.SurveyResults[0].result.email,
      "birthdate": t.SurveyResults[0].result.birthdate
    }
  })
})

console.log(obj);