节点排序自动生成的列(不强调created_at,updated_at)

时间:2019-12-01 04:43:42

标签: node.js sequelize.js

我是续集的新手,我在命令行中指定了“ --underscored”来创建模型。

这是用户模型:

    'use strict';
module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define('User', {
        email: DataTypes.STRING,
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING,
        company_id: DataTypes.INTEGER
    }, {
        underscored: true,
    });
    User.associate = function (models) {
        // associations can be defined here
        User.belongsTo(models.Company);
        User.belongsToMany(models.WorkingDay, { through: 'UsersWorkingDays', as: 'days' })
    };
    return User;
};

这是公司模式:

'use strict';
module.exports = (sequelize, DataTypes) => {
    const Company = sequelize.define('Company', {
        name: DataTypes.STRING
    }, {
        underscored: true,
    });
    Company.associate = function (models) {
        // associations can be defined here
        Company.hasMany(models.User, { as: 'employees'})
    };

    return Company;
};

这些是数据库: enter image description here enter image description here

这是API

router.get("/:id", async (req, res) => {

    let company = await Company.findByPk(req.params.id, { include: ['employes'] });

    res.json(company);
});

这是结果:

{
    "id": 13,
    "name": "Huel - Kihn",
    "createdAt": "2019-12-01T03:55:41.000Z",
    "updatedAt": "2019-12-01T03:55:41.000Z",
    "employees": [
        {
            "id": 8,
            "email": "Randi54@hotmail.com",
            "first_name": "Koby",
            "last_name": "Jaskolski",
            "company_id": 13,
            "createdAt": "2019-12-01T03:55:41.000Z",
            "updatedAt": "2019-12-01T03:55:41.000Z",
            "CompanyId": 13
        },
        {
            "id": 9,
            "email": "Patience.Schuppe63@gmail.com",
            "first_name": "Althea",
            "last_name": "O'Hara",
            "company_id": 13,
            "createdAt": "2019-12-01T03:55:41.000Z",
            "updatedAt": "2019-12-01T03:55:41.000Z",
            "CompanyId": 13
        }
    ]
}

如您所见,未强调结果中的createdAt和updatedAt。但是强调了这两个专栏。有什么方法可以使续集使用数据库列名吗?

在“员工”字段中还有一个额外的“ CompanyId”列,如何防止显示?

我试图在线搜索,但是没有找到任何解决方法。

感谢您的帮助!

0 个答案:

没有答案