Axios throwing Internal server error but api url works - node.js

I hope you are all safe.
My problem:
the URL is working fine, there is no undefined URL error there. when i entered good credentials, it works fine and take me to the location i wish. But anytime i am trying to view the errors, it doesn't appear. Even, in the if condition i try to console.log(res.data.errors) but nothing shows. Even console.log("Hello it not working") doesn't work at all in the if condition... I am confused. Would u help please? but the else works fine. what am i doing wrong please?
import React, { useState } from "react";
import { Link } from "react-router-dom";
import SideBar from "../components/SideBar";
import "./profil.css";
import axios from "axios";
function Profil() {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const handleLogin = (e) => {
e.preventDefault();
const emailError = document.querySelector(".email.error");
const passwordError = document.querySelector(".password.error");
axios({
method: "post",
url: `${process.env.REACT_APP_API_URL}api/user/login`,
withCredentials: true,
data: {
email,
password,
},
})
.then((res) => {
if (res.data.errors) {
emailError.innerHTML = res.data.errors.email;
passwordError.innerHTML = res.data.errors.password;
} else {
window.location = "/";
}
})
.catch((err) => {
if (err.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);
} else if (err.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(err.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', err.message);
}
console.log(err.config);
});
};
return (
<>
<SideBar />
<div className="form-container">
<input type="checkbox" id="flip" />
<div className="cover">
<div className="front">
<img src="dog-4977599_1920.jpg" alt="" />
<div className="text">
<span className="text-i">Welcome to ChablisLAB</span>
<span className="text-j">Please Login before continue</span>
</div>
</div>
<div className="back">
<img className="backImg" src="diary-92652_1920.jpg" alt="" />
<div className="text">
<span className="text-i">Welcome to ChablisLAB</span>
<span className="text-j">Just a few step to gain access</span>
</div>
</div>
</div>
<form>
<div className="form-content">
<div className="login_form">
<div className="title">Login</div>
<div className="input_boxes">
<div className="input_box">
<i className="bx bx-envelope"></i>
<input
type="email"
onChange={(e) => setEmail(e.target.value)}
value={email}
placeholder="Enter your email"
/>
</div>
<div className="email error"></div>
<div className="input_box">
<i className="bx bxs-lock"></i>
<input
type="password"
onChange={(e) => setPassword(e.target.value)}
value={password}
placeholder="Password"
/>
</div>
<div className="password error"></div>
<div className="box_forgot_pass">
<Link to="#">Forgot password?</Link>
</div>
<div className="button input_box">
<input onClick={handleLogin} type="submit" value="Login" />
</div>
<div className="text sign-up-text">
Don't have an account?
<label htmlFor="flip">Signup now</label>
</div>
</div>
</div>
</div>
</form>
</div>
</>
);
}
export default Profil;
.error{
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

I think you need to console res.data.errors before you add if condition. There might be the chance you are putting conditions on the wrong properties which you are not getting in return as a response.
axios({
method: "post",
url: `${process.env.REACT_APP_API_URL}api/user/login`,
withCredentials: true,
data: {
email,
password,
},
})
.then((res) => {
console.log(res.data.errors, res.data); //check like this then put condition if you are getting res.data.errors property in response
if (res.data.errors) {
emailError.innerHTML = res.data.errors.email;
passwordError.innerHTML = res.data.errors.password;
} else {
window.location = "/";
}
})

Related

I was unable to make a post request with Redux-Toolkit RTK Query

I started learning RTK Query few days ago, and I would like to make a post request to my backend. But for some reason, my backend is not accepting the data from my frontend and I keep getting an error that says name, email and password is incorrect. Nothing is wrong with my backend because I was using useContext before and everything works fine.
Here is my where I made the request
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import { createApi } from "#reduxjs/toolkit/query/react";
import axiosBaseQuery from "./axiosBaseQuery";
export const fetcherApi = createApi({
reducerPath: "fetcherApi",
baseQuery: axiosBaseQuery({
baseUrl: "http://localhost:5000/",
}),
endpoints(build) {
return {
registerUser: build.mutation({
//I tried putting the "form" inside a curly braces bracket, it still keep throwing error
query: (form) => ({
url: "register",
method: "post",
body: form,
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}),
}),
};
},
});
export const {
useRegisterUserMutation,
} = fetcherApi;
Here is my frontend, where I was passing the data. I submitted the form in the submitUser function
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import Link from "next/link";
import React, { useState } from "react";
import { AiOutlineCar } from "react-icons/ai";
import { BsEye, BsEyeSlash } from "react-icons/bs";
import { useRouter } from "next/router";
import { useRegisterUserMutation } from "#/store/ReduxStore/fetcherApi";
function Login() {
const [registerUser] = useRegisterUserMutation();
const [showPassword, setShowPassword] = useState("password");
const [name, setName] = useState("");
const [nameError, setNameError] = useState("");
const [email, setEmail] = useState("");
const [emailError, setEmailError] = useState("");
const [password, setPassword] = useState("");
const [passwordOneError, setPasswordOneError] = useState("");
const [secPassword, setSecPassword] = useState("");
const [secPasswordError, setSecPasswordError] = useState("");
const router = useRouter();
const form = {
name,
email,
password,
};
const nameChange = (e) => {
setName(e.target.value);
e.target.value < 1 ? setNameError("Name is required") : setNameError("");
};
const emailChange = (e) => {
setEmail(e.target.value);
e.target.value < 1 ? setEmailError("Name is required") : setEmailError("");
};
const passwordOneChange = (e) => {
setPassword(e.target.value);
e.target.value < 1
? setPasswordOneError("Name is required")
: setPasswordOneError("");
};
const passwordTwoChange = (e) => {
setSecPassword(e.target.value);
e.target.value < 1
? setSecPasswordError("Name is required")
: setSecPasswordError("");
e.target.value !== password
? setSecPasswordError("Password didnot match")
: setSecPasswordError("");
};
const showPasswordHandler = () => {
if (showPassword === "password") {
setShowPassword("text");
} else {
setShowPassword("password");
}
};
const submitUser = async (e) => {
e.preventDefault();
secPassword !== password
? setSecPasswordError("Password didnot match")
: setSecPasswordError("");
//form was submitted here
await registerUser(form);
router.push("/profile");
};
const thedate = new Date();
const year = thedate.getFullYear();
return (
<div className="authbackground">
<div className="flex flex-col mt-3 mx-auto w-4/5 items-center justify-center">
<div className="flex-shrink-0 cursor-default p-2 flex items-center">
<AiOutlineCar className="text-8xl mx-auto text-blue-400" />
<h1 className="text-white text-6xl mx-auto font-logo-font">Big-C</h1>
</div>
<div className="bg-white shadow rounded xl:w-1/3 md:w-1/2 w-full p-10 mt-16">
<p
aria-label="Login to your account"
className="text-2xl font-extrabold leading-6 text-gray-800"
>
Create an account
</p>
<form onSubmit={submitUser}>
<div className="mt-5">
<label htmlFor="name" className="authformlabel">
Name
</label>
{/*_________Name_______*/}
<input
placeholder="Enter email adress"
type="name"
className="authform"
onChange={nameChange}
value={name}
onBlur={() =>
name < 1 ? setNameError("Name is required") : setNameError("")
}
/>
<p className="formerror">{nameError}</p>
</div>
<div className="mt-5">
<label htmlFor="email" className="authformlabel">
Email
</label>
{/*_________Email_______*/}
<input
placeholder="Enter email adress"
type="email"
className="authform"
onChange={emailChange}
value={email}
onBlur={() =>
email < 1
? setEmailError("Email is required")
: setEmailError("")
}
/>
<p className="formerror">{emailError}</p>
</div>
<div className="mt-6 w-full">
<label htmlFor="passwordOne" className="authformlabel">
Password
</label>
<div className="showpasswordicon">
{/*_________PasswordOne_______*/}
<input
placeholder="Enter Password"
type={showPassword}
className="authform"
onChange={passwordOneChange}
value={password}
onBlur={() =>
password < 1
? setPasswordOneError("Password is required")
: setPasswordOneError("")
}
/>
<button
type="button"
onClick={showPasswordHandler}
className="formshowpassword"
>
{showPassword === "password" && <BsEye />}
{showPassword === "text" && <BsEyeSlash />}
</button>
</div>
<p className="formerror">{passwordOneError}</p>
<label htmlFor="passwordTwo" className="authformlabel">
Re-type Password
</label>
<div className="showpasswordicon">
{/*_________PasswordTwo_______*/}
<input
placeholder="Re-enter Password"
type={showPassword}
className="authform"
onChange={passwordTwoChange}
value={secPassword}
onBlur={() =>
secPassword < 1
? setSecPasswordError("Password is required")
: setSecPasswordError("")
}
/>
<button
type="button"
onClick={showPasswordHandler}
className="formshowpassword"
>
{showPassword === "password" && <BsEye />}
{showPassword === "text" && <BsEyeSlash />}
</button>
</div>
<p className="formerror">{secPasswordError}</p>
</div>
<div className="mt-8">
<button aria-label="create my account" className="authformbutton">
Create an account
</button>
</div>
</form>
<p className="text-sm mt-4 font-medium leading-none text-gray-500">
Already have an account?{" "}
<span
role="link"
className="text-sm font-medium leading-none underline text-gray-800 cursor-pointer"
>
<Link href="/login">Login here</Link>
</span>
</p>
</div>
</div>
<footer className="py-16 flex flex-col justify-center items-center">
<p className="mt-2 text-xs xl:text-sm leading-none text-gray-50">
<span>{year}</span> Big-C. All Rights Reserved.
</p>
</footer>
</div>
);
}
export default Login;
Pls kindly help me out. Thanks
I was able to solve it by changing body to data and I use the function for submitting the form in both button and form. Like this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
export const fetcherApi = createApi({
reducerPath: "fetcherApi",
baseQuery: axiosBaseQuery({
baseUrl: "http://localhost:5000/",
}),
endpoints(build) {
return {
registerUser: build.mutation({
//I tried putting the "form" inside a curly braces bracket, it still keep throwing error
query: (form) => ({
url: "register",
method: "post",
//I change body to data
data: form,
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}),
}),
};
},
});
//Here is the function for submitting
const submitUser = async (e) => {
e.preventDefault();
secPassword !== password
? setSecPasswordError("Password didnot match")
: setSecPasswordError("");
await registerUser(form);
router.push("/profile");
setName("");
setEmail("");
setPassword("");
setSecPassword("");
};
//So I put it inside both button and form
<form onSubmit={submitUser}>
{/*All your input ....*/}
<button onClick={submitUser}></button>
<form/>

network error in axios on node.js request

I have this:
class ServicesPage extends Component {
constructor(props) {
super(props);
this.state = {
bookID: '',
bookTitle: '',
bookAuthor: '',
};
}
handleInputChange = e => {
this.setState({
[e.target.name]: e.target.value,
});
};
handleSubmit = e => {
e.preventDefault();
const { siteLocation, Services, date, cnum } = this.state;
const selections = {
siteLocation,
Services,
date,
cnum,
};
axios
.post('http://localhost:3001/updateServices', selections)
.then(() =>
console.log("updating"),
window.location = "/services"
)
.catch(function(error) {
console.log(error)
window.location = "/"
})
};
render() {
return (
<div className="App">
<div id="form-main">
<div id="form-div">
<form onSubmit={this.handleSubmit} className="form">
<select name="siteLocation" id="siteLocation" onChange={this.handleInputChange} >
<option value="ottawa">ottawa</option>
<option value="markham">markham</option>
</select>
<p class="email">
<input name="Services" type="text" class="validate[required,custom[email]] feedback-input" id="Services" placeholder="Services" onChange={this.handleInputChange} />
</p>
<input type="hidden" name="date" value={Date().toLocaleString()} onChange={this.handleInputChange} />
<p class="cnum">
<input name="cnum" type="text" class="validate[required,custom[email]] feedback-input" id="cnum" placeholder="cnum" onChange={this.handleInputChange} />
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</div>
</div>
);
}
}
export default ServicesPage;
As you can see, I am updating the state when a field is edited, and then sending it to the backend through the axios call.
and then in my backend,
app.post('/updateServices', (req, res) => {
console.log(req.body.Services)
})
when I press submit, and try to run the axios call, it gives me this error in my console:
Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:84)
I am not sure why this is happening. any ideas? I have cors installed backend, and everything is configured properly, but I can't seem to submit.
You should send something in response to your API, like so.
app.post('/updateServices', (req, res) => {
console.log(req.body.Services)
res.send({message: "success"})
})
otherwise, your request is not resolved and you will not get anything on the frontend.

react login register form sends empty data

I have a backend setup with node and express and frontend is React + Redux
The backend works well. I tested it all with insomina and i can successfully register a user and login.
On the front end side, everything looks ok except for the fact that when i try to login or register a user, it tells me that the input fields are required, which means that it sends an empty object to my backend. I also have a message in the console saying that componentWillReceiveProps(nextProps) is depricated and suggests using getDerivedStateFromProps instead. The issue is that i am new to React and have no idea how to convert that block. Can someone tell me? Is that even why the form sends an empty object?
EDIT: Added github repo for complete code
https://github.com/onidemon/wh-evcharge
Register.js is below, both login and Register have a componentWillReceiveProps block which i think might be causing the issue. Not sure how to convert them to getDerivedStateFromProps if that is even the cause of the problem.
import React, { Component } from "react";
import { Link, withRouter } from "react-router-dom";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { registerUser } from "../../actions/authActions";
import classnames from "classnames";
class Register extends Component {
constructor() {
super();
this.state = {
name: "",
email: "",
password: "",
password2: "",
errors: {}
};
}
componentDidMount() {
// If logged in and user navigates to Register page, should redirect them to dashboard
if (this.props.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({
errors: nextProps.errors
});
}
}
onChange = e => {
this.setState({ [e.target.id]: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const newUser = {
name: this.state.name,
email: this.state.email,
password: this.state.password,
password2: this.state.password2
};
this.props.registerUser(newUser, this.props.history);
};
render() {
const { errors } = this.state;
return (
<div className="container">
<div className="row">
<div className="col s8 offset-s2">
<Link to="/" className="btn-flat waves-effect">
<i className="material-icons left">keyboard_backspace</i> Back to
home
</Link>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<h4>
<b>Register</b> below
</h4>
<p className="grey-text text-darken-1">
Already have an account? <Link to="/login">Log in</Link>
</p>
</div>
<form noValidate onSubmit={this.onSubmit}>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.name}
error={errors.name}
id="name"
type="text"
className={classnames("", {
invalid: errors.name
})}
/>
<label htmlFor="name">Name</label>
<span className="red-text">{errors.name}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.email}
error={errors.email}
id="email"
type="email"
className={classnames("", {
invalid: errors.email
})}
/>
<label htmlFor="email">Email</label>
<span className="red-text">{errors.email}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.password}
error={errors.password}
id="password"
type="password"
className={classnames("", {
invalid: errors.password
})}
/>
<label htmlFor="password">Password</label>
<span className="red-text">{errors.password}</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.password2}
error={errors.password2}
id="password2"
type="password"
className={classnames("", {
invalid: errors.password2
})}
/>
<label htmlFor="password2">Confirm Password</label>
<span className="red-text">{errors.password2}</span>
</div>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<button
style={{
width: "150px",
borderRadius: "3px",
letterSpacing: "1.5px",
marginTop: "1rem"
}}
type="submit"
className="btn btn-large waves-effect waves-light hoverable blue accent-3"
>
Sign up
</button>
</div>
</form>
</div>
</div>
</div>
);
}
}
Register.propTypes = {
registerUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
errors: state.errors
});
export default connect(
mapStateToProps,
{ registerUser }
)(withRouter(Register));
Login.js is below
import React, { Component } from "react";
import { Link } from "react-router-dom";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { loginUser } from "../../actions/authActions";
import classnames from "classnames";
class Login extends Component {
constructor() {
super();
this.state = {
email: "",
password: "",
errors: {}
};
}
componentDidMount() {
// If logged in and user navigates to Login page, should redirect them to dashboard
if (this.props.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.auth.isAuthenticated) {
this.props.history.push("/dashboard");
}
if (nextProps.errors) {
this.setState({
errors: nextProps.errors
});
}
}
onChange = e => {
this.setState({ [e.target.id]: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const userData = {
email: this.state.email,
password: this.state.password
};
this.props.loginUser(userData);
};
render() {
const { errors } = this.state;
return (
<div className="container">
<div style={{ marginTop: "4rem" }} className="row">
<div className="col s8 offset-s2">
<Link to="/" className="btn-flat waves-effect">
<i className="material-icons left">keyboard_backspace</i> Back to
home
</Link>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<h4>
<b>Login</b> below
</h4>
<p className="grey-text text-darken-1">
Don't have an account? <Link to="/register">Register</Link>
</p>
</div>
<form noValidate onSubmit={this.onSubmit}>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.email}
error={errors.email}
id="email"
type="email"
className={classnames("", {
invalid: errors.email || errors.emailnotfound
})}
/>
<label htmlFor="email">Email</label>
<span className="red-text">
{errors.email}
{errors.emailnotfound}
</span>
</div>
<div className="input-field col s12">
<input
onChange={this.onChange}
value={this.state.password}
error={errors.password}
id="password"
type="password"
className={classnames("", {
invalid: errors.password || errors.passwordincorrect
})}
/>
<label htmlFor="password">Password</label>
<span className="red-text">
{errors.password}
{errors.passwordincorrect}
</span>
</div>
<div className="col s12" style={{ paddingLeft: "11.250px" }}>
<button
style={{
width: "150px",
borderRadius: "3px",
letterSpacing: "1.5px",
marginTop: "1rem"
}}
type="submit"
className="btn btn-large waves-effect waves-light hoverable blue accent-3"
>
Login
</button>
</div>
</form>
</div>
</div>
</div>
);
}
}
Login.propTypes = {
loginUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
errors: state.errors
});
export default connect(
mapStateToProps,
{ loginUser }
)(Login);
EDIT: Added AuthActions.js
import axios from "axios";
import setAuthToken from "../utils/setAuthToken";
import jwt_decode from "jwt-decode";
import { GET_ERRORS, SET_CURRENT_USER, USER_LOADING } from "./types";
// Register User
export const registerUser = (userData, history) => dispatch => {
axios
.post("/api/users/register", userData)
.then(res => history.push("/login"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
// Login - get user token
export const loginUser = userData => dispatch => {
axios
.post("/api/users/login", userData)
.then(res => {
// Save to localStorage
// Set token to localStorage
const { token } = res.data;
localStorage.setItem("jwtToken", token);
// Set token to Auth header
setAuthToken(token);
// Decode token to get user data
const decoded = jwt_decode(token);
// Set current user
dispatch(setCurrentUser(decoded));
})
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
// Set logged in user
export const setCurrentUser = decoded => {
return {
type: SET_CURRENT_USER,
payload: decoded
};
};
// User loading
export const setUserLoading = () => {
return {
type: USER_LOADING
};
};
// Log user out
export const logoutUser = () => dispatch => {
// Remove token from local storage
localStorage.removeItem("jwtToken");
// Remove auth header for future requests
setAuthToken(false);
// Set current user to empty object {} which will set isAuthenticated to false
dispatch(setCurrentUser({}));
};
I looked on your github project.
If I am not mistaken I haven't seen you using in your server the bodyparser.json() function. You only use the bodyParser.urlencoded(), which convert url parameters into processable objects.
Add as well bodyParser.json(), then you would get the incoming data from the body of the request as well. Then you should have it I believe.

React, Node: Variable shows undefined on app.post()

Not sure if I've done it correctly on the server or client side, but in the client side when I console.log(values) they appear, but in server, I get undefined. But when i do enter the right credentials for a user in the database i get status:ok, so something is found. And also a extra bug is i cannot to the post request twice, then i get the error: Error: Cannot enqueue Handshake after invoking quit.
Client Code:
class Login extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
const email = data.get('email');
const password = data.get('password');
fetch('/authentication', {
method: 'POST',
body: JSON.stringify({
"email": email,
"password": password
}),
}).then(function (response) {
if (response.ok) {
alert("Login Successful!");
} else {
console.log("Failed");
}
});
console.log("Email: " + email);
console.log("pwd: " + password);
}
render() {
return (
<form className="col s12 m12 l12" onSubmit={this.handleSubmit}>
<div className="row">
<div className="col s12 center-align">
<img className="logo" src="images/bibdata-center.png" alt="BibData Logo"/>
</div>
<div className="input-field col s12">
<i className="material-icons prefix">email</i>
<input id="email" name="email" type="email" className="validate" onChange={this.handleEmailChange}/>
<label htmlFor="email">Email</label>
</div>
</div>
<div className="row">
<div className="input-field col s12">
<i className="material-icons prefix">lock</i>
<input id="password" name="password" type="password" className="validate" onChange={this.handlePasswordChange}/>
<label htmlFor="password">Password</label>
</div>
</div>
<div className="row">
<div className="col s12 m6 l6 center-align">
<button className="btn waves-effect btn-large waves-light" type="submit" value="Submit" name="action">Sign in
<i className="material-icons right">send</i>
</button>
</div>
<div className="col s12 m6 l6 center-align">
<button className="btn waves-effect btn-large waves-light blue" type="submit" name="action"
onClick={() => renderReactComponent(newUser, 'root')}>
Register
</button>
</div>
</div>
<div className="row">
<div className="col s12 center-align">
<a href="#" onClick={() => renderReactComponent(forgotPassword, 'root')}>
<span className="helper-text grey-text text-darken-1"
data-error="wrong" data-success="right">
Forgot password?
</span>
</a>
</div>
</div>
</form>
);
}
}
And Server Code:
app.post('/authentication', function (req, res) {
connection.connect();
let email = req.body.email;
let password = req.body.password;
console.log(email);
console.log(password);
connection.query('SELECT * FROM user WHERE email = ? AND password = ?',
[email, password], function (error, results, fields) {
if (error) {
return res.status(500).send(error);
}
return res.send({ error: false, data: results, message: 'ok'})
});
connection.end();
});
All help will be appreciated!
When you make your fetch call to /authentication endpoint, you are sending serialized JSON data in your request body. The server however has no means of knowing that you have actually sent an JSON. When posting JSON, you must indicate that by providing appropriate content-type header.
In this case your fetch options (2nd param) should have one more property like this:
...
headers: {
"Content-Type": "application/json",
},
...
This is assuming, you have already setup body-parser or another similar module on your server side code which can parse JSON request body.

Using this.refs is deprecated error when trying to use this.refs.value

II am trying to do a post request to the database to post an object called questions using "react-dom": "^15.6.1". The data might be something as follows:
{description: 'What is E-commerce?', ismeeting: false, expID: '123A2'}
What i am trying to do is take the "description" , "ismeeting" and ,"expID" values from a form and a checkbox (checkbox for "ismeeting") in the front end and pass it to the backend. To get the description value for instance; i am using this.refs.description.value. However i am getting an error Using this.refs is deprecated in the onSubmit(e) function and Using string literals in ref attributes is deprecated react/no-string-refs in the render() function
Here is the OnSubmit code.
onSubmit(e) {
const newQues = {
description: this.refs.description.value,
ismeeting: this.refs.check_me.checked,
expID: this.refs.expID.value
};
this.addQues(newQues);
e.preventDefault();
}
and here is the render() code.
render() {
return (
<div>
<br/>
<h1> DO NOT HESISTATE TO ASK OUR EXPERTS </h1>
<form onSubmit={this.onSubmit.bind(this)}>
<div className="input-field">
<input type="text" name="description" ref="description"/>
<label htmlFor="description"> Description </label>
</div>
<div className="input-field">
<input type="text" name="expID" ref="expID"/>
<label htmlFor="name"> expID </label>
</div>
<div className="checkbox">
<label>
<input type="checkbox" name="ismeeting" ref="check_me" />Meeting
</label>
</div>
<input type ="submit" value="ASK" className="btn" />
</form>
</div>
);
}
finally this is the full code.
import React, { Component } from 'react';
import axios from 'axios';
import '../Styles.scss';
class Questions extends Component {
addQues(newQues) {
console.log(newQues);
axios.request({
method: 'Post',
url: 'http://localhost:3001/api/Questions',
data: newQues
}).then(response => {
}).catch(err => console.log(err));
}
constructor() {
super();
this.state = {
Questions: []
};
}
onSubmit(e) {
const newQues = {
description: this.refs.description.value,
ismeeting: this.refs.check_me.checked,
expID: this.refs.expID.value
};
this.addQues(newQues);
e.preventDefault();
}
render() {
return (
<div>
<br/>
<h1> DO NOT HESISTATE TO ASK OUR EXPERTS </h1>
<form onSubmit={this.onSubmit.bind(this)}>
<div className="input-field">
<input type="text" name="description" ref="description"/>
<label htmlFor="description"> Description </label>
</div>
<div className="input-field">
<input type="text" name="expID" ref="expID"/>
<label htmlFor="name"> expID </label>
</div>
<div className="checkbox">
<label>
<input type="checkbox" name="ismeeting" ref="check_me" />Meeting
</label>
</div>
<input type ="submit" value="ASK" className="btn" />
</form>
</div>
);
}
}
export default Questions;
String refs have been deprecated. So what you need to do is update your refs
<input type="text" name="expID" ref="expID"/>
should be updated to
setExpIdRef = (r) => this.expIdRef = r;
onSubmit = (e) => {
const newQues = {
expID: this.expIdRef.value
};
// Do what you need to with newQuest i.e call your database
}
render() {
...
<input type="text" name="expID" ref={this.setExpIdRef}/>
}
The best solution is to make your inputs controlled inputs. Where you keep track of the value in the state.
constructor() {
super();
this.state = {
expID: ''
};
}
onExpIdChange = (e) => {
this.setState({
expID: e.target.value
})
}
onSubmit = (e) => {
const newQues = {
expID: this.state.expID
};
// Do what you need with the newQues object
}
render() {
...
<input type="text" name="expID" onChange={this.onExpIdChange} />
}

Resources