从数据到数据的JSON响应中的sequelize.js-include方法获取错误的对象名称



我有一个属于daily_entry表的表report_data,但当我像这样调用api来获取daily_entry表的所有数据时,它会像下面的一样发送响应

输出

{
"response_code": "0",
"message": "Operation is successfully executed",
"status": "success",
"data": {
"id": 1,
"user_id": 1,
"date": "12-10-2020",
other data ....
"is_active": true,
"createdAt": "2020-10-21T06:25:57.877Z",
"updatedAt": "2020-10-21T06:25:57.877Z",
"report_datum": {
"id": 1,
"entry_id": 1,
"Date": null,
"report_document_id": "2",
"createdAt": "2020-10-21T06:26:02.642Z",
"updatedAt": "2020-10-21T06:26:02.642Z"
}
},
"level": "info",
"timestamp": "2020-10-21T06:25:45.947Z"
}

预期

{
"response_code": "0",
"message": "Operation is successfully executed",
"status": "success",
"data": {
"id": 1,
"user_id": 1,
"date": "12-10-2020",
other data ....
"is_active": true,
"createdAt": "2020-10-21T06:25:57.877Z",
"updatedAt": "2020-10-21T06:25:57.877Z",
"report_data": {
"id": 1,
"entry_id": 1,
"Date": null,
"report_document_id": "2",
"createdAt": "2020-10-21T06:26:02.642Z",
"updatedAt": "2020-10-21T06:26:02.642Z"
}
},
"level": "info",
"timestamp": "2020-10-21T06:25:45.947Z"
}

我已经在我的整个项目中检查过,没有像report_datum这样的名字,是sequelize.js的bug还是其他任何方法

API I代码低于

getdaily_entryById: async (req, res) => {
sequelize.sequelize.transaction(async (t1) => {
if (!req.params.id) {
logger.warn(error.MANDATORY_FIELDS)
return res.status(500).send(error.MANDATORY_FIELDS);
}
let data = await sequelize.daily_entry.findOne({
where: { id: req.params.id },
include: [
sequelize.report_data
]
});
let result = error.OK
result.data = data
logger.info(result);
return res.status(200).send(result);
}).catch(function (err) {
logger.warn(err)
console.log(err)
return res.status(500).send(error.SERVER_ERROR);
});
}, 

我使用了冻结表名称,但仍在更改名称

var db_instance = new Sequelize(config.DB.database, config.DB.username, config.DB.password, {
host: config.DB.host,
dialect: config.DB.dialect,
define: {
timestamps: true,
freezeTableName: true
},
logging: false
});

更新

每日条目表

module.exports = function (sequelize, DataTypes) {
const daily_entry = sequelize.define('daily_entry ', {
user_id: {
type: DataTypes.INTEGER(),
allowNull: true
},
date: {
type: DataTypes.STRING,
allowNull: true
},
report_status: {
type: DataTypes.STRING,
allowNull: true
},
high_close: {
type: DataTypes.DOUBLE(),
allowNull: true
},
high_open: {
type: DataTypes.DOUBLE(),
allowNull: true
},
low_close: {
type: DataTypes.DOUBLE(),
allowNull: true
},
low_open: {
type: DataTypes.DOUBLE(),
allowNull: true
},
is_active: {
type: DataTypes.BOOLEAN,
allowNull: true
}
});
return daily_entry 
};

报告_数据

module.exports = function (sequelize, DataTypes) {
const report_data= sequelize.define('report_data', {
daily_entry : {
type: DataTypes.INTEGER(),
allowNull: true
},
Date: {
type: DataTypes.INTEGER(),
allowNull: true
},
report_document_id: {
type: DataTypes.TEXT,
allowNull: true
}
},
{
tableName: 'report_data'
});
return report_data
};

你能试试这个吗!我希望它能解决你的问题

let data = await sequelize.daily_entry.findOne({
where: { id: req.params.id },
include: [
{
model:sequelize.report_data,
as:'report_data',
required:true
}
]
});

@NikhilPonduri忘记提到一件事你必须在你的relation中添加as:'report_data',然后你可以使用下面的代码,它肯定会工作

let data = await sequelize.daily_entry.findOne({
where: { id: req.params.id },
include: [
{
model:sequelize.report_data,
as:'report_data',
required:true
}
]
});

最新更新