最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

从通过sequelize另一个模型添加属性和值

IT培训 admin 3浏览 0评论

从通过sequelize另一个模型添加属性和值

使用sequelize我想,我的结果(JSON)看起来像IM(见:WantResult)。是否有可能通过对矫正我的功能找回正是JSON?

这里是我的功能:

try {
          const mediImport = await User.findOne({

            where: { Id: 1 },
            // Select forename as Vorname, name as Nachname
            attributes: [['forename', 'Vorname'], ['name', 'Nachname']],
            include: {
              model: SurveyResult,
              attributes: ['result', 'result'] }

          })

下面是我的结果:

    {
    "Person": {
        "Vorname": "Mustermann",
        "Nachname": "Max",
        "SurveyResult": {
            "result": {
                "birthdate": "01.01.1990",
                "Sind Sie miteinander verheiratet?": "Ja",
                "Waren Sie wegen Ihres Kinderwunsches bereits in ärztlicher Behandlung?": "Ja"
            }
        }
    }
}

WantResult:但我想,我的结果看起来像

Person": {
        "Vorname": "Mustermann",
        "Nachname": "Max",
        "birthdate": "01.01.1990" 
回答如下:

你必须做这样的事情(这工作,因为birthdaySurveyResult一个属性,是不是里面result的情况下):

try {
  const mediImport = await User.findOne({

    where: { Id: 1 },
    // Select forename as Vorname, name as Nachname
    attributes: [
        ['forename', 'Vorname'], 
        ['name', 'Nachname'],
        [sequelize.literal('"SurveyResult"."birthdate"'), 'birthdate']
    ],
    include: {
        model: SurveyResult,
        attributes: [] 
    }
  })
}

从通过sequelize另一个模型添加属性和值

使用sequelize我想,我的结果(JSON)看起来像IM(见:WantResult)。是否有可能通过对矫正我的功能找回正是JSON?

这里是我的功能:

try {
          const mediImport = await User.findOne({

            where: { Id: 1 },
            // Select forename as Vorname, name as Nachname
            attributes: [['forename', 'Vorname'], ['name', 'Nachname']],
            include: {
              model: SurveyResult,
              attributes: ['result', 'result'] }

          })

下面是我的结果:

    {
    "Person": {
        "Vorname": "Mustermann",
        "Nachname": "Max",
        "SurveyResult": {
            "result": {
                "birthdate": "01.01.1990",
                "Sind Sie miteinander verheiratet?": "Ja",
                "Waren Sie wegen Ihres Kinderwunsches bereits in ärztlicher Behandlung?": "Ja"
            }
        }
    }
}

WantResult:但我想,我的结果看起来像

Person": {
        "Vorname": "Mustermann",
        "Nachname": "Max",
        "birthdate": "01.01.1990" 
回答如下:

你必须做这样的事情(这工作,因为birthdaySurveyResult一个属性,是不是里面result的情况下):

try {
  const mediImport = await User.findOne({

    where: { Id: 1 },
    // Select forename as Vorname, name as Nachname
    attributes: [
        ['forename', 'Vorname'], 
        ['name', 'Nachname'],
        [sequelize.literal('"SurveyResult"."birthdate"'), 'birthdate']
    ],
    include: {
        model: SurveyResult,
        attributes: [] 
    }
  })
}
发布评论

评论列表 (0)

  1. 暂无评论