gpt4 book ai didi

javascript - 在 Console.log 和 api 响应中获取未定义的外键

转载 作者:行者123 更新时间:2023-12-03 22:17:03 24 4
gpt4 key购买 nike

我有属于 report_data 表的 daily_entry 表,但是当我调用 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_i": 1, <<<<<<<----------OUTPUT
"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,<<<<<-------------EXPECTED
"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"
}
两个表之间的关系是 hasOne
db.daily_entry.hasOne(db.report_data, { onDelete: "cascade", foreignKey: 'entry_id', foreignKeyConstraint: true, targetKey: 'id' });
我已经记录了来自 db 的数据,如下所示
 dataValues:{id: 1, entry_i: 1, daily_entry : 8468476, date: 23-10-2020, …}
get entry_id:ƒ () {\n return this.get(attribute);\n }
undefined
我已经检查了我的整个项目,没有像 entry_i 这样的名字
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);
});
}
daily_entry 表模式
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

};
report_data 表模式
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
};
那么,我哪里做错了,为什么我将 entry_i 作为外键

最佳答案

尝试将您的表名更改为 CamelCase 然后尝试。
我知道这应该是评论,但此时我没有评论的特权

关于javascript - 在 Console.log 和 api 响应中获取未定义的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64494605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com