Right now when I want to add an object to the database I do the following:
return BSRequest.create({
salon_name: req.body.salon_name,
salon_type: req.body.salon_type,
employees: req.body.employees,
postcode: req.body.postcode,
city: req.body.city,
website: req.body.website,
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
phone_number: req.body.phone_number
})
.then(bsRequest => res.status(201).send(bsRequest))
.catch(error => res.status(400).send(error));
Is there a way in Javascript/Node to automatically get the properties of req.body
so that these can be mapped to the key of the object? Or some other way that I can simplify it and have less code.
I'm using the sequelize ORM.
if req.body and your database db model keys are same you can just write
return BSRequest.create(req.body).then(bsRequest => res.status(201).send(bsRequest)) .catch(error => res.status(400).send(error));
Comment: I'll not recommend that you directly store values in req.body to the database. Please do include type-check
for values you get from frontend before saving them to the database. You can do that by making some modification in Akshay's answer.