gpt4 book ai didi

mysql - 错误: SequelizeValidationError: string violation: created cannot be an array or an object

转载 作者:行者123 更新时间:2023-11-29 16:01:24 27 4
gpt4 key购买 nike

我正在实现 MERN Stack 登录/注册,并尝试逐步在 Postman 中测试我的响应。首先,我编写了注册代码,然后编写了登录代码,但在调用注册链接的情况下,我在 postman 中收到以下错误:

错误:SequelizeValidationError:字符串违规:创建的不能是数组或对象

有人可以提供任何建议来帮助吗?我认为在 User.js findone() 函数中,我这边有某种失误。

还有其他解决办法吗?

./database/DB.js

const db = {}
const sequelize = new Sequelize("mern", "root", "", {
host: "localhost",
dialect: "mysql",
port: "3307",
operatorsAliases: false,

pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
})

db.sequelize = sequelize
db.sequelize = sequelize

module.exports = db

./models/User.js

const db = require("../database/db")

module.exports = db.sequelize.define(
'user',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
created: {
type: Sequelize.STRING
}
},
{
timestamps: false
}
);

./routes/User.js

const users = express.Router()
const cors = require('cors')
const jwt = require("jsonwebtoken")
const bcrypt = require('bcrypt')

const User = require("../models/User")
users.use(cors())

process.env.SECRET_KEY = 'secret'

users.post('/register', (req, res) => {
const today = new Date()
const userData = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: req.body.password,
created: today
}

User.findOne({
where: {
email: req.body.email
}
})
.then(user => {
if(!user){
bcrypt.hash(req.body.password, 10, (err, hash) => {
userData.password = hash
User.create(userData)
.then(user => {
res.json({status: user.email + ' registered'})
})
.catch(err => {
res.send('error: ' + err)
})
})
} else {
res.json({error: "User already exists"})
}
})
.catch(err => {
res.send('error: ' + err)
})
})

users.post('/login', (req, res) => {
User.findOne({
where: {
email: req.body.email
}
})
.then(user => {
if(user) {
if(bcrypt.compareSync(req.body.password, user.password)) {
let token = jwt.sign(user.dataValues, process.env.SECRET_KEY, {
expiresin: 1440
})
res.send(token)
}
} else {
res.status(400).json({error: 'User does not exist'})
}
})
.catch(err => {
res.status(400).json({ error: err})
})
})

module.exports = users

package.json

  "name": "login-registration",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^3.0.6",
"bcryptjs": "^2.4.3",
"body-parser": "^1.17.2",
"cors": "^2.8.4",
"express": "^4.16.3",
"jsonwebtoken": "^7.4.2",
"mysql": "^2.14.1",
"mysql2": "^1.6.1",
"nodemon": "^1.18.3",
"sequelize": "^4.38.0"
}
}

服务器.js

var cors = require ('cors')
var bodyParser = require("body-parser")
var app = express()
var port = process.env.PORT || 5000

app.use(bodyParser.json())
app.use(cors())
app.use(bodyParser.urlencoded({extended: false}))

var Users = require('./routes/users')

app.use('/users', Users)

app.listen(port, () => {
console.log("Server is running at port: " + port)
})

最佳答案

在./routes/User.js中,在/post注册路由下,userData对象有一个带有today属性的已创建字段,该字段是一个Date对象。

在 ./models/User.js 中,您指定创建的类型应为 Sequelize.STRING。

这就是导致错误的矛盾。当您调用 User.create(userData) 时,它会给出该错误,因为输入参数的类型错误。

要解决此问题,您需要创建一种 Sequlize.Date 类型,或者将今天的日期对象转换为字符串。

const today =  new Date().toJSON();

Date 类有许多与字符串不同的函数。您应该选择最适合您的 here

关于mysql - 错误: SequelizeValidationError: string violation: created cannot be an array or an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56173419/

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