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

标签 node.js postgresql sequelize.js

我有表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_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的错误还是有其他方法

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

关于node.js - 从sequelize.js获取错误的对象名称,包括从数据到数据的JSON响应中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64457909/

相关文章:

node.js - 大多数 RESTFUL API 函数无法进行单元测试

postgresql - 为 Postgresql 配置 Grails 3

python - 如何将数据类型从 python pandas 映射到 postgres 表?

node.js - Sequelize 如何不在每次服务器启动时重新定义模型

mysql - BelongsToMany 与 Sequelize 表不关联

javascript - "migration:create"和 "migration:generate"之间有什么区别?

javascript - 如何将 aws-sdk-js 捆绑到无服务器框架优化包中?

javascript - 修改作为方法内部参数传递的对象是一种不好的做法吗?

postgresql - 为什么数据库连接会自动关闭?

node.js - 在Docker中使用负载测试应用