Saturday, 18 January 2020

Node JS MySQL Help

Hello everyone,Hope you all are having a great weekend. I am currently building a small node and SQL CRUD application and am having a small issue I hope someone can help and guide me.​I have a MySQL table called 'Employee which has the ID to be autoincremented. I am using the MySQL module in Node to make calls to this table through my server. Sorry if my terminology is a bit weak as I am new to this. In my query which is in Node as a post to create a new employee, I cannot give the record an ID as I do not want when the user will be using this from the front end to create a user having to enter an ID as well. Therefore i chose to use an auto increment. The problem is, if for instance the last record in the table has an ID of '3'. And if my post request which does not have an ID as it is automatically put in by SQL has an ID of '7', additional records are being inserted to cover the gap. So if the first record should be '3' and the next '7', due to auto increment it is becoming 3,4,5,6,7 witch 4 5 6 being extra records with the same post request data I have sent via Insomnia. I could obviously switch of autoincrement but the post request I am using or my user will be using via a front end will have to enter the ID as well which i do not want. Can anyone please guide me. I have posted my simple Node Script for this functionality below.​app.post('/employees', (req, res) => {var post = [{EmployeeID: last_insert_id(),FirstName: req.body.FirstName,LastName: req.body.LastName,EmailID: req.body.EmailID,Role: req.body.Role}];db.query("Insert into Employee SET ?" , post, (err, rows, fields) => {if (!err)res.send(rows);elseconsole.log(err);})});

Submitted January 19, 2020 at 03:40AM by Liverpool1900

No comments:

Post a Comment