I’m developing a full-stack application (React, Node, Postgres). When receiving the form data from the frontend, I expect certain variable names sush as firstName
and I map firstName
to first_name
on the backend server to insert it into the postgres db. Now my question is regarding the GET requests and sending the data back to the front end:
Should I map the names back to the frontend naming convention on the server before I send the json (map first_name
back to firstName
) or handle the mapping on the frontend?
What is the best practice regarding such situations?
Question posted in Javascript
A very good W3school tutorial can be found here.
A very good W3school tutorial can be found here.
2
Answers
In your SQL queries you can get the data renamed:
The object returned from the query will have the properties named correctly.
Yes, the best practice is to keep the code for the mapping in a single place: either in the backend, or in the frontend, not both. Keep the naming convention within each API consistent.
If your backend accepts
firstName
as input and does the mapping internally when inserting in the database, it should also do the mapping internally when reading from the database and providefirstName
as output.