微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在Console.log和api响应中获取未定义的外键

我有一个属于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","report_data": {
            "id": 1,"entry_id": 1,<<<<<-------------EXPECTED
            "Date": null,"timestamp": "2020-10-21T06:25:45.947Z"
}

两个表之间的关系为hasOne

db.daily_entry.hasOne(db.report_data,{ onDelete: "cascade",foreignKey: 'entry_id',foreignKeyConstraint: true,targetKey: 'id' });

我已经记录了来自数据库的数据,就像下面一样

 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);
    });
}

每日条目 表架构

module.exports = function (sequelize,DataTypes) {

    const daily_entry = sequelize.define('daily_entry ',{
    user_id: {
        type: DataTypes.INTEGER(),allowNull: true
    },date: {
        type: DataTypes.STRING,report_status: {
        type: DataTypes.STRING,high_close: {
        type: DataTypes.DOUBLE(),high_open: {
        type: DataTypes.DOUBLE(),low_close: {
        type: DataTypes.DOUBLE(),low_open: {
        type: DataTypes.DOUBLE(),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(),Date: {
        type: DataTypes.INTEGER(),report_document_id: {
        type: DataTypes.TEXT,allowNull: true
    }
 },{
        tableName: 'report_data'
    });
 return report_data
};

所以,我在哪里做错了,为什么要使用entry_i作为外键

解决方法

尝试将表名更改为 CamelCase ,然后尝试。

我知道这应该是评论,但目前我没有评论的权限

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。