'MY SQL Sequelize Unique Value not working as intended
I am trying to code a website to work with ReactJS, NodeJs and MySQL.
I have been using Sequelize and it works but i realised that it allows all usernames even those that are used.
i set the value to be Unique in the JS file. I have followed this website (https://sequelize.org/docs/v6/core-concepts/validations-and-constraints/) but to no success.
module.exports = (sequelize,DataTypes) =>{
const Users = sequelize.define("Users", {
username:{
type: DataTypes.TEXT,
allownull: false,
unique: true
},
userpass:{
type: DataTypes.STRING,
allownull: false,
},
usergroup:{
type: DataTypes.STRING,
allownull: false,
},
});
return Users;
}
The code i am running
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const mysql = require('mysql2');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "testschema"
})
const db = require('./models');
// Routers
const userRouter = require('./routes/user-routes');
app.use("/users", userRouter);
app.post('/testing', (req, res) => {
console.log("testing route");
console.log(req.body);
res.send("Testing Route");
})
app.post('/login', async (req, res) => {
const username = req.body.username
const userpass = req.body.userpass
console.log("Line 9", req.body)
console.log("backend triggered");
const loginInfo = await new Promise((resolve, reject) => {
connection.query("SELECT * FROM users WHERE username = ? AND userpass = ?", [username, userpass], (err, result) => {
if (err) {
console.log(err)
}
else {
if (result.length > 0) {
console.log("Success" + result);
resolve(result)
}
else {
console.log("Wrong username/password")
}
}
})
});
console.log(loginInfo)
})
;
app.post('/register', async (req, res) => {
const username = req.body.username
const userpass = req.body.userpass
console.log("Line 83", req.body)
console.log("backend triggered");
const registerInfo = await new Promise((resolve, reject) => {
connection.query("INSERT INTO users (username, userpass,createdAt,updatedAt) VALUES (?,?,0,0)", [username, userpass], (err, result) => {
if (err) {
console.log(err)
}
})
});
console.log(registerInfo)
})
connection.connect((err) => {
if (err) {
console.log("Error occurred", err);
} else {
console.log("Connected to MySQL Server");
}
});
db.sequelize.sync().then(() => {
app.listen(8000, () => {
console.log("Server is up on port 8000");
});
});
This is what i get in the backend
Connected to MySQL Server
Executing (default): CREATE TABLE IF NOT EXISTS `Users` (`id` INTEGER NOT NULL auto_increment , `username` TEXT UNIQUE, `userpass` VARCHAR(255), `usergroup` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `Users` FROM `testschema`
Server is up on port 8000
Line 83 { username: 'blueflag', userpass: 'blueflag' }
backend triggered
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|