How can I display my response messages that I send in node js on my frontend with Reactjs - node.js

I am trying to build a basic login/signup app with the mern stack.I am new to it. While making the backend with node and express I have added lot of json messages in the code like-
return res.status(400).json({message:"Unable to find user by this id"})
return res.status(404).json({message:"User doesnt exist"});
How can I display this json data to my frontend with react?
This is the handleSubmit code-
const handleSubmit=(e)=>{
e.preventDefault()
console.log(inputs);
if(isSignup){
sendRequest("signup")
.then((data)=>localStorage.setItem("userId",data.user._id))
.then(()=>dispath(authActions.login()))
.then(()=>navigate("/blogs"))
}else{
sendRequest()
.then((data)=>localStorage.setItem("userId",data.user._id))
.then(()=>dispath(authActions.login()))
.then(()=>navigate("/blogs"))
.then(data=>console.log(data))
}
}
This is the return div code-
<form onSubmit={handleSubmit}>
<Box display="flex" flexDirection={'column'}
alignItems='center'
maxWidth={400}
justifyContent={'center'}
boxShadow="10px 10px 20px #ccc"
padding={3}
margin="auto"
marginTop={5}
borderRaidus={5}>
<Typography variant="h2" padding={3} textAlign="center">{isSignup?"Signup":"Login"}</Typography>
{isSignup && <TextField name="name" onChange={handleChange} value={inputs.name} placeholder='name' margin="normal"/>}
<TextField name="email" onChange={handleChange} value={inputs.email} type={'email'} placeholder='email' margin='normal'/>
<TextField name="password" onChange={handleChange} value={inputs.password} type={'password'} placeholder='password' margin='normal'/>
<Button type={"submit"} variant="contained" sx={{borderRadius:3,marginTop:3}} color="warning">Submit</Button>
<Button onClick={()=>setIsSignup(!isSignup)} sx={{borderRadius:3,marginTop:3}}>
Change to {isSignup ?"Login":"Signup"}</Button>
</Box>
</form>
</div>
)
}
export default Auth

Just try this one.
const handleSubmit = async(...) => {
...
try{
const response = await axios.post(...);
const data = await response.json();
console.log(data);
}catch(){
...
}
}

Related

.map() React undefined - Axios & Express

Keep getting this error when trying to submit a form in React
Uncaught TypeError: queryList.map is not a function
This is the code:
const [query, setQuery] = useState();
const [queryList, setQueryList] = useState([]);
const [response, setResponse] = useState([]);
const [responseList, setResponseList] = useState([]);
const createQuery = () =>{
setQueryList(
{query}
);
alert(queryList)
Axios.post('http://localhost:3001/createQuery', {
query
}).then((res)=>{
setResponse(res)
setResponseList(...responseList, {res})
})
}
return(
<div>
{queryList && queryList.map((e) => {
return(
<p className="ml-52 text-white text-xl">{e.query}</p>
)
})}
<form>
<textarea onChange={(event)=>{
setQuery(event.target.value)
}}
type="text" name="name" autoComplete="off" placeholder="Ask a question" className = "caret-gray-200 bg-gray-800 shadow-md h-20 w-5/6 inset-x-0 bottom-6 absolute left-36 items-center snap-center text-xl p-6" />
<button onClick={createQuery} type="submit" name="submit" className="text-white inset-x-0 bottom-6 absolute bg-transparent w-20 h-20 ml-auto mr-28 focus:outline-none focus:none">
<AiOutlineSend size="28" />
</button>
</form>
</div>
)
After I submit the form, I need the query mapped
I know I didn't mention any other errors, but if you see them, please let me know!
By the way, all the other posts didn't work for me.
Thanks :)
When you set the queryList you will need to set it to an array by using array brackets. Also by the name i'm assuming you want an array of all queries so you will need to include previous queries that are already stored in queryList.
setQueryList([...queryList, { query }]);
This is what your setter function should look like.
Aside from this the alert function will not work since queryList is not updated in time to be used so I would recommend you to just use query in alert instead of queryList.
Also because of the way you use forms the page will be redirected, this is solved by using onSubmit event and using the preventDefault() function.
const [query, setQuery] = useState();
const [queryList, setQueryList] = useState([]);
const [response, setResponse] = useState();
const [responseList, setResponseList] = useState([]);
const createQuery = (event) => {
event.preventDefault();
setQueryList([...queryList, { query }]);
alert(query);
Axios.post('http://localhost:3001/createQuery', {
query,
}).then((res) => {
setResponse(res);
setResponseList(...responseList, { res });
});
};
return (
<div>
{queryList &&
queryList.map((e) => {
return (
<p className='ml-52 text-white text-xl'>{e.query}</p>
);
})}
<form onSubmit={createQuery}>
<textarea
onChange={(event) => {
setQuery(event.target.value);
}}
type='text'
name='name'
autoComplete='off'
placeholder='Ask a question'
className='caret-gray-200 bg-gray-800 shadow-md h-20 w-5/6 inset-x-0 bottom-6 absolute left-36 items-center snap-center text-xl p-6'
/>
<button
type='submit'
name='submit'
className='text-white inset-x-0 bottom-6 absolute bg-transparent w-20 h-20 ml-auto mr-28 focus:outline-none focus:none'
/>
</form>
</div>
);
I have not tested anything with axios but this code should work.

TypeError: Cannot read properties of undefined (reading 'id') - React App

I've trying to edit an entry of the data fetched from mysql and want to reflect if back in mysql db. For this im using post method of Axios. But im getting the following error when i try to edit the data and see for changes.
TypeError: Cannot read properties of undefined (reading 'id')
at C:\Users\Rahul01\Desktop\DBMS-PROJECT\BusTicketManagementSystem\server\index.js:123:23
at Layer.handle [as handle_request] (C:\Users\Rahul01\Desktop\DBMS-PROJECT\BusTicketManagementSystem\server\node_modules\express\lib\router\layer.js:95:5)
Here are the Codes
1.index.js
app.post('/admin/updatebus',(res,req)=>{
const id=req.body.id
const name=req.body.newbusname
const fromcity=req.body.newfromstation
const tocity=req.body.newtostation
const capacity=req.body.newcapacity+1
db.query("UPDATE bus SET busname=?,fromcity=?,tocity=?,capacity=? WHERE busid=? ",[name,fromcity,tocity,capacity,id+1],(err,result)=>{
if(err)
{
console.log(err);
res.send({op:f})
}
else
{
res.send({op:s});
}
})
})
2.Editbus.js
import Axios from 'axios'
import React, { useState } from 'react'
import { useParams } from 'react-router-dom'
export default function Editbus() {
const [newbusname, setnewbusname] = useState('')
const [newfromstation, setnewfromstation] = useState('')
const [newtostation, setnewtostation] = useState('')
const [newcapacity, setnewcapacity] = useState(0)
const {id}=useParams();
const handlesave=(e)=>{
// alert(key)
alert('Name'+newbusname+"\n From:"+newfromstation+"\nTo: "+newtostation+"\n Capacity: "+newcapacity)
Axios.post('http://localhost:3001/admin/updatebus',{
newbusname:newbusname,
newfromstation:newfromstation,
newtostation:newtostation,
newcapacity:newcapacity,
id:parseInt(id)
}).then((resp)=>{
if(resp.data.op==='s')
{
alert('Updated')
}
else
{
alert('Not Updated')
}
})
e.preventDefault()
}
return (
<>
<div class="container-contact100">
<div class="wrap-contact100">
<form class="contact100-form validate-form">
<span class="contact100-form-title">
Edit Bus {id}
</span>
<div class="wrap-input100 validate-input" data-validate="Name is required">
<input class="input100" type="text" name="name" onChange={e=>setnewbusname(e.target.value)} placeholder="New Bus Name" required/>
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex#abc.xyz">
<input class="input100" type="text" name="email" onChange={e=>setnewfromstation(e.target.value)} placeholder="From Bus Stop" required/>
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex#abc.xyz">
<input class="input100" type="text" name="email" onChange={e=>setnewtostation(e.target.value)} placeholder="To Bus Stop" required/>
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex#abc.xyz">
<input class="input100" type="number" name="email" onChange={e=>setnewcapacity(e.target.value)} placeholder="New Capacity" required/>
<span class="focus-input100"></span>
</div>
<button onClick={handlesave}>Update</button>
</form>
</div>
</div>
<div id="dropDownSelect1"></div>
</>
)
}
In App.js
<Route exact path="/admin/editbus/:id" element={}>
It should be (req, res) instead of (res,req) in the .post handler.
Corrected Code:
app.post('/admin/updatebus',(req,res)=>{}
First Parameter should be for Request and second one for the Response.
Tip:
Include type='button' in the Submit Button so that the HTML form doesn't gets submitted.
<button type="button" onClick={handlesave}>Update</button>

Trying to display IMG with src set to Binary/Buffer image data from Mongoose

Trying to display IMGs with React/JSX with Buffered/Binary data saved to my MongoDB/Mongoose database.
i iterate over the data array with the IMG element looking like this:
<img src={`data:${item.img.contentType};base64,${item.img.data.data.toString("base64")}`} alt="" />
import React, { useState, useEffect } from "react";
import axios from "axios";
const FilesUpload = () => {
const [allPics, setAllPics] = useState([]);
useEffect(() => {
const getPics = async () => {
let res = await axios.get("http://localhost:8080/");
setAllPics(res.data);
};
// query the server for all of the picture objects
getPics();
}, []);
const [state, setState] = useState({});
const handleChange = (e) => {
e.preventDefault();
setState(e.target.value);
console.log(state);
};
return (
<>
<h1>upload an image</h1>
<hr />
<div>
<form
action="http://localhost:8080/"
method="POST"
encType="multipart/form-data"
>
<div>
<label>Image Title</label>
<input type="text" id="name" placeholder="Name" name="name" onChange={handleChange} value={state.name}/>
</div>
<div>
<label htmlFor="desc">Image Description</label>
<textarea id="desc" name="desc" rows="2" placeholder="Description"
onChange={handleChange} value={state.desc}/>
</div>
<div>
<label htmlFor="image">Upload Image</label>
<input type="file" id="image" name="image" required />
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
</div>
{allPics.length > 0 ? (
<div>
{allPics.map((item, index) => (
<div key={index}>
<div>
<img src={`data:${item.img.contentType};base64,${item.img.data.data.toString("base64")}`} alt="" />
</div>
</div>
))}
</div>
) : (
<>
<hr />
<br />
<h1>uploaded files</h1>
<h5>Loading..</h5>
</>
)}
</>
);
};
export default FilesUpload;
but I always get ERR_INVALID_URL:
from similar threads on the net I've read that I need to take those comma-delimitated values and remove the comma which will give me the proper data. having a hard time figuring that out. any help would be great. thanks
I was facing the same problem after saving image like this in my mongoose model and after many research I resolved this, hope this works for you also
const base64String = btoa(String.fromCharCode(...new Uint8Array(item.img.data.data)));
and in img tag put this -:
src={`data:image/${item?.img?.contentType};base64,${base64String}`}

How to create onSubmit with Material-UI

So I'm fairly new to node.js / react / material-ui. I've been following a guide to try setting up a website and have been getting on pretty well. I decided to include material-ui for snazzy components (not part of the guide) and then got stuck because I can't seem to fire custom functions whilst using a themed ui.
With the below code, I can create everything fine if I ditch the 'classes' props. I can add my function and everything works. But I obviously lose all my styling if I do this.
const styles = theme => ({
// Styling - omitted
});
const Login = (props) => {
const {classes} = props;
return(
<div>
<Paper className={classes.root}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="email"
label="Email"
className={classes.textField}
InputProps={{
className: classes.input
}}
type="email"
name="email"
autoComplete="email"
margin="normal"
variant="outlined"
required
autoFocus
/>
<TextField
id="outlined"
label="Password"
className={classes.textField}
InputProps={{
className: classes.input
}}
type="password"
autoComplete="current-password"
margin="normal"
variant="outlined"
required
/>
<Typography className={classes.divider} />
<Button
type="submit"
variant="contained"
color="inherit"
className={classes.button}
>
Login
</Button>
</form>
</Paper>
</div>
);
}
Login.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(Login);
I'm trying to combine the styling as well as being able to fire a custom function to submit the form data. Does anyone have any thoughts?
The class property/ styles shouldn't have any effect on form submission custom function. If you think that ditching the 'class props' you can get a custom function to work - post your code so we can help you further. [Your code is missing submit function]
Here is one way to add custom submit function
const Login = (props) => {
const {classes} = props;
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
function handleSubmit(event) {
event.preventDefault();
console.log( 'Email:', email, 'Password: ', password);
// You should see email and password in console.
// ..code to submit form to backend here...
}
return( <div >
<Paper className={classes.root}>
<form className={classes.container} onSubmit={handleSubmit} >
<TextField
....
value={email}
onInput={ e=>setEmail(e.target.value)}
.....
/>
<TextField
....
value={password}
onInput={ e=>setPassword(e.target.value)}
....
/>
<Typography className={classes.divider} />
<Button
type="submit"
....
className={classes.button}
>
Login
</Button>
</form>
</Paper>
</div>
);

Handling uploads multiple images from react js

Good Morning, i got a problem about some codes, fyi im still new to react js, i try to uploads multiple images from react to express and save it to mongoDB
, i already re-search that i need to use multer in my node js, but i dont know the argument need to pass because in react i have 1 name of field of files to upload, but on my submit data has 2 data to send.
handleSubmit(e){
e.preventDefault();
// var formData = new FormData();
// formData.append('file', e.target.value)
// formData.append('file2', e.target.value)
let {imagePreviewUrl} = this.state;
const dataCategory = this.state.dataCategory;
let categoryName = this.refs.categoryName.value;
let categoryImage = this.refs.categoryImage.value;
let categoryDesc = this.refs.categoryDesc.value,
categoryImageCabor = (<img alt="www.google.com" height="100px" src={imagePreviewUrl} />),
namaCabor = this.refs.namaCabor.value,
descCabor = this.refs.descCabor.value,
imageCabor = (<img height="100px" src={this.state.imageCabor.props.src} />)
fetch('http://localhost:4000/add', {
mode:'cors',
method:'post',
headers:{
'Content-Type' : 'application/json',
// "Accept" : "application/json",
// "type" : "formData"
},
body:JSON.stringify({
categoryName : categoryName,
categoryDesc : categoryDesc,
categoryImage: categoryImage,
categoryImageCabor : categoryImageCabor,
namaCabor : namaCabor,
descCabor : descCabor,
imageCabor : imageCabor,
status : true
}),
}).then(res=>{
return res.json();
}).catch(function(err){
if(err){
console.log(err);
}
})
this.setState({
dataCategory : dataCategory,
imagePreviewUrl : false,
});
this.refs.myForm.reset();
this.refs.myForm.focus();
}
handleChange(e){
this.setState({
[e.target.categoryName] : e.target.value,
[e.target.categoryImage] : e.target.value,
[e.target.categoryDesc] : e.target.value
})
}
render(){
let {imagePreviewUrl} = this.state;
let $imagePreview = null;
if(imagePreviewUrl){
$imagePreview = (<img alt ="www.google.com" height="100px" src={imagePreviewUrl} />)
}
return this.state.imageCabor === "" ? <div></div> : (
<div>
<h3>Insert Category Cabang Olahraga </h3>
<form style={{marginTop: 10}} ref="myForm" onSubmit={this.handleSubmit} >
<div className="form-group">
<label>Nama Category</label>
<input
name="categoryName"
type="text"
className="form-control"
ref="categoryName"
onChange={this.handleChange}
/>
</div>
<div className="form-group">
<label>Deskripsi Category </label>
<textarea
name="categoryDesc"
type="text"
className="form-control"
ref="categoryDesc"
rows="5"
onChange={this.handleChange}
/>
</div>
<div className="form-group">
<label>Upload Icon Image</label> <br />
<div>{$imagePreview}</div>
<input
name="categoryImage"
type="file"
ref="categoryImage"
className="image-control"
onChange={this.imageHandleChange}
/>
</div>
<h1>Cabang Olahraga</h1>
<div className ="form-group">
<label>Nama Cabang Olahraga</label>
<input
name="namaCabor"
type="text"
className="form-control"
ref="namaCabor"
value={this.state.namaCabor}
/>
</div>
<div className ="form-group">
<label>Deskripsi Cabang Olahraga</label>
<textarea
name="descCabor"
type="text"
className="form-control"
ref="descCabor"
rows="5"
value={this.state.descCabor}
/>
</div>
<div className="form-group">
<label>Icon Cabang Olahraga</label> <br />
<div><img height="100px" src={this.state.imageCabor.props.src} /></div>
</div>
<div className="form-group">
<input type="submit" value="Apply" className ="btn btn-primary" />
</div>
</form>
</div>
);
}
//node js code
//i dont know the argument need to pass in here
app.post('/', upload.array('catagoryImage', 12), (req, res, next) =>{
const files = req.files;
if(!files){
const error = new Error('Please choose files')
error.httpStatusCode = 400
return next(err)
}
res.send(files);
}}
To send images along with data from frontend to backend, you need to wrap it inside FormData object like this:
let formData = new FormData();
formdata.append('keyName','value') //replace keyName with your key and value with its value.
And do not set any headers in your fetch request. This should do. Try it and let me know.
Hope it helps!!

Resources