node.js - createdAt 不能是数组或对象

标签 node.js sequelize.js

有人可以帮助我我在使用 sequelize 在数据库中插入记录时遇到问题,created_at 和 updated_at 会自动填充,并且出现以下错误:

"name": "SequelizeValidationError",
"errors": [
    {
        "message": "createdAt cannot be an array or an object",
        "type": "string violation",
        "path": "createdAt",
        "value": "2020-05-28T22:04:45.023Z",
        "origin": "CORE",
        "instance": {
            "id": null,
            "placeId": "1",
            "userId": 3,
            "name": "Alex Santos",
            "email": "customer@gmail.com",
            "documento": "00000000000",
            "phone": "(37)98596-9869",
            "updatedAt": "2020-05-28T22:04:45.023Z",
            "createdAt": "2020-05-28T22:04:45.023Z"
        },
        "validatorKey": "not_a_string",
        "validatorName": null,
        "validatorArgs": []
    },
    {
        "message": "customer.updateAt cannot be null",
        "type": "notNull Violation",
        "path": "updateAt",
        "value": null,
        "origin": "CORE",
        "instance": {
            "id": null,
            "placeId": "1",
            "userId": 3,
            "name": "Alex Santos",
            "email": "customer@gmail.com",
            "documento": "031.682.546-81",
            "phone": "(37)98596-9869",
            "updatedAt": "2020-05-28T22:04:45.023Z",
            "createdAt": "2020-05-28T22:04:45.023Z"
        },
        "validatorKey": "is_null",
        "validatorName": null,
        "validatorArgs": []
    }
]

}

有人可以提供任何建议以提供帮助吗?我认为在 customer.js 中 create() 函数在我这边有某种想念。

能有其他解决办法吗?

客户.js
   /* jshint indent: 1 */

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('customer', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'id'
        },
        placeId: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            references: {
                model: 'places',
                key: 'id'
            },
            field: 'place_id'
        },
        userId: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            },
            field: 'user_id'
        },
        customerId: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            field: 'customer_id'
        },
        name: {
            type: DataTypes.STRING(100),
            allowNull: false,
            field: 'name'
        },
        email: {
            type: DataTypes.STRING(255),
            allowNull: false,
            field: 'email'
        },
        documento: {
            type: DataTypes.STRING(16),
            allowNull: false,
            defaultValue: '',
            field: 'documento'
        },
        phone: {
            type: DataTypes.STRING(12),
            allowNull: false,
            field: 'phone'
        },
        createdAt: {
            type: DataTypes.STRING(255),
            allowNull: false,
            field: 'created_at'
        },
        updateAt: {
            type: DataTypes.STRING(255),
            allowNull: false,
            field: 'update_at'
        }

    }, {
        tableName: 'customer'
    });
};

客户 Controller .js
const Customers = require('./../models/custumers')
const Sequelize = require('sequelize');
const db = require('../../bin/database');
const Customer = new Customers(db, Sequelize);
exports.create = async(req, res) => {
    decodeToken = await jwt.decodeToken(req.headers['x-access-token'])
    let userId = decodeToken.id
    console.log(new Date())
    let { placeId, customerId, name, email, documento, phone } = req.body
    Customer.create({
            placeId,
            userId,
            customerId,
            name,
            email,
            documento,
            phone
        })
        .then((Customer) => {
            return res.status(200).send({ Customer: Customer })
        }).catch(err => {
            res.status(423).send(err);
        });
}

最佳答案

错误消息告诉您出了什么问题。您将 createdAt 列定义为字符串,但看起来您正在发送一个对象(很可能是日期类型)。 Date 类型的值不是字符串,因此 Sequelize 会使用 "validatorKey": "not_a_string""type": "string violation" 等信息警告您。

我不知道你为什么选择将时间戳存储为字符串,但修复的一部分是将列更改为日期类型。

您还提到列值是自动填充的,但我没有看到任何地方会发生这种情况。除非您明确为该列设置默认值,然后在提交 session 时不提供值,否则不会自动填充这些列。

简短的回答是,将列类型更改为 DATE,并确保在提交到数据库之前在值上设置时间戳。

关于node.js - createdAt 不能是数组或对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62075555/

相关文章:

node.js - 密码哈希+盐如何工作

node.js - VSCode 错误地将 Sequelize findOne 和 findAll 的返回类型推断为 any

javascript - 如何处理 Sequelize 嵌套/复合查询?

node.js - AWS lambda 搞乱了我的编码

node.js - Sequelize v6.2.0 : include. model.getTableName 不是函数

sequelize.js - 如何使用 sequelize.js 创建替换操作?

sequelize.js - Sequelize include 不能在多对多与带有类型列的槽表上正常工作

node.js - 使用 Google API 在工作表中设置数据时,Google 表格会自动更改日期格式

node.js - 尝试结束 Express js 路由中的代码执行

javascript - req.headers ['authorization' ] 在 Nodejs JWT(JSON WEB TOKEN)中未定义