Getting wrong object name from sequelize.js include method in JSON response from data to datum

Viewed 248

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 responce 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_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"
}

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

I have checked in my whole project there is no name like report_datum is it bug of sequelize.js or any there is any other method

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

I have used freeze table name but still getting name changed

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

Update

daily_entry table

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

Can you try this one! I Hope it will solve your problem

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

@NikhilPonduri forgot to mention one thing that You have To add as:'report_data' in your relation to than you can use below code and it will definitely work

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