I'm making a react registry with some fields (idStudent (primary key and auto-increment), first name, last name, ..., teacher, prerequisites) and I'm using Formik & yes for verification.
Later, I had to link my application with a recommendation system (recommend final year projects to students) based on prerequisites and grades obtained in certain subjects.
In the beginning, I only used one table to store data from the front-end form,
Student (First Name, Last Name, ..., Prerequisites)
Students can select prerequisites from the response selection that contains prerequisites according to the instructor (Example: If the student studies Computer Science, the response selection will only show Computer Science prerequisites such as Reaction, Angular, Machine Learning, ...). Considering that a student can have multiple prerequisites, the prerequisite column in the student table will contain multiple ids for the selected prerequisite, The prerequisites are stored in another table in my database (prerequisites(idFaculty, idPreconditions, prerequisites)) I know that I can use a JSON file to store multiple ids in a column, but after doing some research on Stackoverflow in some previous posts, I found that processing JSON is difficult, especially if I want to update a column. So I created another table to store the prerequisites
selected by students when registering (studentPrecessions(idStd(foreign key referencing idStudent from students table), idPrecession(foreign key referencing idPrecessions from prerequisites table))
The problem I am facing is how to send a request for two posts through axios, considering Thought maybe I should use a loop to store multiple rows in case students select multiple prerequisites.
This is what I did:
My backend files
app.post("/registerStudent", (req, res) => { const faculty = req.body.faculty; const firstName = req.body.firstName; const lastName = req.body.lastName; const phone = req.body.phone; const email = req.body.email; const password = req.body.password; db.query( "INSERT INTO students (email, password, firstName, lastName, faculty, phone) VALUES (?,?,?,?,?,?)", [email, password, firstName, lastName, filiere, phone], (err, result) => { if (err) { console.log(err); } else { // store chosen prerequisites //result.insertId is the current idStudent of the student who registering const idStd = result.insertId; const idPrerequisite = req.body.idprerequis; db.query( "INSERT INTO studentPrerequisites (idStd, idPrerequisite) VALUES (?,?)", [idFiliere, idPrerequisite], (err, result) => { if (err) { console.log(err); } else { res.send("Values Inserted"); } } ); } } ); });
My front-end code
const onSubmit = (values, actions) => { Axios.post("http://localhost:3001/registerStudent", { faculty: values.faculty, firstName: values.firstName, lastName: values.lastName, phone: values.phone, email: values.email, password: values.password, }) .then(() => { //preId is an array that contains the selected prerequisites(id's) from react select //I try to use a for loop to store multiple of prerequisites dynamically in case a //student select multiple prerequisites for (let i = 0; i < preId.length; i++) { idPrerequisites: preId[i], } }) .then(() => { console.log("success!"); }); actions.resetForm(); };
P粉9767371012024-03-27 18:04:50
It is best to let the backend handle multiple prerequisites by passing an array of prerequisites to your request. I would also use Knex and async/await to avoid a lot of .then chaining and take advantage of transactions. If any error occurs in the transaction, the transaction will resume all queries. Knex also makes it super easy to query your database with built-in methods without having to write raw SQL. You should also use object destructuring instead of doing firstName = req.body.firstName, lastName = req.body.lastName, etc. You can learn more about knex and connecting databases to it here: https://knexjs.org/guide/#node-js Also, why don't you hash your passwords? This is at least the most basic security you should do!