I have written a route to register customers like this:
customerRouter.post(
'/register',
expressAsyncHandler( async (req, res) => {
const { firstname, lastname, Email, Password } = req.body;
const registerCustomer = await Customers.create({
firstName: firstname,
lastName: lastname,
email: Email,
password: Password
})
if (!registerCustomer) {
res.status(401).send({
message: "Invalid Customer data",
});
}else{
res.send({
id: registerCustomer.id,
firstname: registerCustomer.firstName,
lastname: registerCustomer.lastName,
email: registerCustomer.email,
isAdmin: registerCustomer.isAdmin,
token: generateToken(registerCustomer)
});
};
})
);
I also have a register screen/page that allows users to register and should send inputs to the backend, the code for the screen is like this:
const RegisterScreen = {
after_render: () =>{
document.getElementById('register-form').addEventListener
('submit', async (e) => {
e.preventDefault();
showLoading();
const data = await register({
firstname: document.getElementById('firstname').value,
lastname: document.getElementById('lastname').value,
email: document.getElementById('email').value,
password: document.getElementById('password').value,
});
hideLoading();
if(data.error){
showMessage(data.error);
}else{
setCustomerInfo(data)
document.location.hash ='/';
}
});
},
render: () => {
if(getCustomerInfo().firstName){
document.location.hash = '/';
}
return `
<section class="menu-section">
<div class="form-container">
<form id="register-form">
<ul class="form-items">
<li>
<h1>Register!</h1>
</li>
<li>
<label for="firstName"> First Name :</label>
<input type="name" name="firstName" id="firstname"/>
</li>
<li>
<label for="lastName"> Last Name :</label>
<input type="name" name="lastName" id="lastname"/>
</li>
<li>
<label for="email"> Email :</label>
<input type="email" name="email" id="email"/>
</li>
<li>
<label for="password">Password : </label>
<input type="password" name="password" id="password"/>
</li>
<li>
<label for="password">Re-Enter Password : </label>
<input type="password" name="repassword" id="repassword"/>
</li>
<li>
<button type="submit" class="btn btn-default">Register!</button>
</li>
<li>
<div>
Already have an account?
<a href="/#/signin"> Sign in!</a>
</div>
</li>
</ul>
</form>
</div>
</section>
`;
},
};
The fields in my database are written like this: “firstName” “lastName” “email” “password” “isAdmin”:
module.exports = (sequelize, DataTypes) => {
const Customers = sequelize.define("Customers", {
firstName: {
type: DataTypes.STRING,
allowNull: false,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
isAdmin: {
type: DataTypes.BOOLEAN,
default: '0',
}
});
return Customers;
};
my server.js looks like this:
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import data from './data';
import customerRouter from './routers/customerRouter'
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use("/api/customers", customerRouter);
const db = require("./models");
app.get('/api/products', (req, res)=>{
res.send(data.products)
})
app.get('/api/products/:id', (req, res) =>{
const product = data.products.find((x) => x.id === req.params.id);
if(product){
res.send(product);
}else{
res.status(404).send({message: 'Product Not Found!'});
}
})
app.use((err, req, res, next)=>{
const status = err.name && err.name === 'ValidationError' ? 400 : 500;
res.status(status).send({message: err.message});
})
db.sequelize.sync().then(() => {
app.listen(5000, () => {
console.log('Server running on port http://localhost:5000');
});
});
When I run my registering route in postman it works fine, and is able to register customers through postman, but when I try to enter the values from screen/frontend I get the error: “notNull Violation: Customers.firstName cannot be. null etc.” for each single field.
I have a signin route and screen that is similar which is able to request data and compare and authenticate it. I´m not to sure where I went wrong. Thanks for any suggestions in advance.