Getting foreign key undefine in Console.log and in api response

Viewed 138

I have table report_data which is belongs to daily_entry table but when I call api like to get all data of daily_entry table it send response like below

output

{
    "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"
}

expected

{
    "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"
}

relation between both table is hasOne

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

I have logged data coming from db it was like below

 dataValues:{id: 1, entry_i: 1, daily_entry : 8468476, date: 23-10-2020, …}
 get entry_id:ƒ () {\n            return this.get(attribute);\n          }
 undefined

I have checked in my whole project there is no name like entry_i

API I code is below

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 table schema

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 tables schema

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

So, Where am I doing wrong and why I am getting entry_i as foreign key

1 Answers

Try To change your table name to CamelCase and then try.

I know this should be comment but at this time I don't have privilege of comment

Related