I am able to add new users using Postman with this model
sequelize.define('users', {
id: {
type: S.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
name: {
type: S.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Name is required'
},
len: {
args: [2, 30],
msg: 'Name must be at least 2 characters long.'
},
isAlpha: true
}
},
lastName: {
type: S.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Lastname is required.'
},
len: {
args: [2, 50],
msg: 'Lastname must be at least 2 characters long.'
},
isAlpha: true,
},
}
})
}
This is the express route I created
Interestingly, the .findOrCreate
function isn't functioning as expected!
I had to split the system into .findOne
and .create
functions.
server.post('/', async (req, res)=>{
try {
const { name, lastName } = req.body;
if(!name || !lastName){
res.send('All fields must be filled out')
}
const user = await Users.findOne({
where: {
email: email
}
})
if(user){
return res.send('This user already exists, please choose a different one!').status(100);
}
const createUser = await Users.create({
name: name,
lastName: lastName
})
return res.send(createUser)
}
catch (err) {
res.send({ data: err }).status(400);
}
})
The issue is that Postman works fine but when I try to insert data into the model through the psql terminal, it throws the following error:
insert into users (name, lastName)
values('John', 'Doe', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea808e858faa8d878b8386c4898587">[email protected]</a>', '12345678', 'wherever');
ERROR: column "lastname" does not exist in table "users"
LINE 1: insert into users (name, lastName)