gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:25:44 30 4
gpt4 key购买 nike

有人可以帮助我我在使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com