React: Store <Form> data from to MySQL - mysql

I have created a register page in reactjs, where I am taking firstName, lastName, password, email from a user.
In the backend, I have created the application using SpringBoot to handle the request from frontend.
Now, I am able to store (temporarily) the register form data in http://localhost:8080/forms .
When the user enters the data in register form at the front end, the data is stored temporarily in the spring boot application in the url mentioned in the axios.
So, when I enter this url (in the browser), am able to see the list of registered users with their credentials in JSON format like this :
[{"firstName":"Bhavya", "lastName":"Gupta", "email":"abcd#a", "password":"zaqxsw","id":100}]
But this data is temporarily stored, so when I restart the server the data is no longer there.
submitBook= event =>{
event.preventDefault();
const book = {
firstName: this.state.firstName,
lastName: this.state.lastName,
password: this.state.password,
email: this.state.email
};
axios.post("http://localhost:8080/forms",book).then(response => {
if(response.data!=null){
this.setState({"show":true});
setTimeout(() => this.setState({"show":false}),3000);
} else {
this.setState({"show":false});
}
});
this.setState(this.initialState);
}
The complete code is available here
I want to store the register form data from front-end (client-side) into a MySQL database, so that I can perform login authentication on the data stored.
I have created MySQL database with 4 columns - firstName, lastName, email, password. I searched on the internet and read several answers in SO Sending data to database in React, referred several blogs and articles, but I am not able to understand how to achieve this.
Can anyone please help me out in solving this issue?

In a typical/modern web application, you have well defined roles/purposes for each part of the system. From your question, it appears that you intend for the roles to be interchanged and that might the source of your troubles here.
Let;s look closely at your set-up:
Client-side/Front-end (JavaScript/React)
This is simply an enabler for a user to interact with your actual application. In your set-up, this should render the form and capture registration data.
Server-side/Back-end (Java/SpringBoot)
This is your application layer where most processing happens (business logic).
In your set-up, this is where your MVC happens. And essentially, after any required validation, this layer then communicates with the Database.
Database-layer (SQL or NoSQL)
This is the layer that you utilize to store and persist data within your app. In your set-up, you are using MySQL to handle your data.
Already your backend is already exposing endpoints which you are consuming at the front-end via axios. Therefore the direct answer to your question lies in getting SpringBoot to work with your MySQL in order to persist your data.
Here is an official tutorial on how to access data with MySQL from the spring team.

For Posting Form data from ReactJS to MySQL database via Spring Boot Application, I have written blogs, that gives a detailed explanation and a step by step method to connect front end part with backend .
Refer to these blogs :
Prerequisites to POST form data from ReactJS to an API endpoint : Part 1
Initial Setup for creating a basic React App: Part 2
Posting Form data from ReactJS to MySQL database via Spring Boot Application: Part 3

Related

Flutter: rendering an UI from JSON and store / map data dynamic

I want to render a UI from a jSON string that has multiple layers.
The user should be able to enter data, which will then be stored and shared, without the overhead of the jSON Render Structure.
However, the assignment of the data must be possible.
The app renders a template from a multidimensional json string that can capture metrics (user inputs). The measured data are entered via text fields by the user.
There are different windows in the app, which are rendered from different json UI-render-files.
The stored algorithms in the windows at the Frontend differ.
The following should be possible:
All windows are created with different jSON strings (works with build_value now).
The user's input is saved. (Currently only works by saving the render json string with the data under a different name (with Package: Shared Preferences)).
The data entered by the user is copied from one window to the other window. (Data Binding / Data Mapping)
The data entered by the user will be sent to a backend.
I only have the idea to use id's in the render json, which allow a mapping.
Are there better solutions?
Saving the entered data is possible by saving the whole jSON - String with an other name.
The goal is to map the data.
It should also be possible for the user to insert another object for the measurement data acquisition at the client / device. The entered data must also be saved.
This sounds like a REST API. Then you would use a frontend framework like angular or react to take user input.
I could build this with a .net core web api with sql db as the backend. this would be a web service (REST API solution) on the backend of your solution.
Next I would integrate Swagger with dependency injection and add authentication.
now its time to create the front end that will use your tested Web Service.
you would have 1 side of your SPA POST new JSON to the Web service in a form.
you would have the other side GET the new record as a new tab with pagination or in a table with pagination.
This would be a good PoC for your idea and will let you learn the parts of the solutions architecture in your language of choice. the "design" would be the same no matter what language your choice.
backend web service can be done in a flask and with a MySQL DB. or with any other combo you have skills with.
the Frontend could be done in knockout.js, making it a little easier to learn than angular or react.
Please create new questions when you choose your software design. I would love to give answers for each:)
I would love to level up with you! ;)

How to dynamically auto populate a input field uisng Nodejs and MySQL?

I have a field in my website where I want users to enter book names that they can checkout from the database. I want it so that when they start typing the names I want suggestions or drop down under the input box matching the name of the book they are typing.
Is it possible to achieve something like this? I have a books table in my MySQL database and I am using Nodejs as my backend. I have searched a lot online but did not find anything related to this online. Therefore, I decided to ask the question here.
You have to send AJAX request with type GET and send the typed character to the Backend.
and in backend you have to do query for ex :select * from book where name like %input%
Then you have to return the result as an array to the front-end.
Finally, in your front-end code you have to render the result array under the input.
Also, you can use any ready jquery plugin to do this task in front-end code.
The UI element is commonly called a "typeahead" or autocomplete.
Twitter release their frontend component as a jQuery plugin called typeahead.js. Most frontend frameworks will have an equivalent plugin or component.
The backend datasource for the books is up to you to implement.
For small datasets you can render the required book data within the page so it is directly accessible from your javascript.
For large datasets you will probably need to create a backend "book search" service in Nodejs. Typeahead libraries can send that user input to a URL via an AJAX request and the service returns the matching results, usually as a JSON object.
Code for geek example for your stack.
You can make a drop-down menu with book names and using ajax you can get value from the input and search in database and display data .
I suggest you to use mongo db as database.
You can find tutorials in w3 schools or
in malayalam https://www.youtube.com/c/Crossroadstalk

feathers.js - saving one record to multiple databases - mysql and elastic search

I have built an app with Feathers.js and I'm trying to find the way, how can I save any record to the multiple databases? I would like to save a "message" for example to the mysql and also to the elastic search.
I would like to use elastic search for "full text" search, but I would also like to have all data saved in some relational database.
I've created a service using:
feathers generate service
but I can select only one specific database there.
Any help will be really appreciated.
Fast way to me is to create another feathers app (microservice) for elasticsearch and post the data to elasticsearch too.
If you are using socket in the frontend app you can define 2 sockets (1 for each micro service).
When you post new data to feathers app 1 (mysql) you can :
on response of the data created post the same data to feathers app 2 (elasticsearch)
on error abort and correct
or
add a service('your_post_service').on('created', ... and then post data to feathers app 2 (elasticearch)

How to fetch all data from table in mysql database in react application

I want to fetch at least 10 user data from MySQL db because I want to add search functionality in my React JS how I fetch at least 10 user data at a time when user scroll it load more data.
Thanks in advance
Db is Structure is something like this
table>user
email:varchar100
pass:varchar100
email:varchar100
and
table>userProfile
displayname:varchar100
profilepic
phonenumber
userType
There are two types of user signup first is VIP and second is non-vip when Vip signup then userType will be VIP and when non-Vip signup so it should be non-Vip so I want non-Vip search VIP user both user data push on same table user and userProfile.
So first of all, React is not made to handle your backend. As I see in the comments, you're using express as a webserver, so I guess it's NodeJS based.
The first thing you have to do is create a route in your backend that will fetch the data from your table. I recommend using REST or GraphQL to do so.
Once your route is set and ready to go (you can test it with tools like Postman), you can then make an HTTP Request from your React app (using fetch or axios as an example) to your backend server, and tada ! 🎉

json login on spring security

I'm building a REST back end based on spring and i'm using spring security to secure the requests. But i'm lookin for an issue to login by sending parameters in json rather than defaults parameters sent by the default login page of spring security.
I'm working with spring security 4.0.1 and spring 4.1
Any issue please?
If you're using just username and password, you can simply add a new filter to the stack, akin to the existing UsernamePasswordAuthenticationFilter, that would react to a specific URL only (just like the default one reacts to j_spring_security_check only), parse the JSON and create the very same UsernamePasswordAuthenticationToken that the default filter creates. This leaves the auth provider the same as the token didn't change.
If you need more fields in addition to username and password, either create a new token type (or use existing one if it makes sense) and a new auth provider that can deal with that token type. You can also just cram extra fields into UsernamePasswordAuthenticationToken using setDetails(), but this is a bit hacky.