Using the useContext , it says object is not iterable (cannot read property Symbol(Symbol.iterator))? - node.js

When ever i am using useContext in the js file, it gives the error "TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))". Removing that line , it works perfectly fine . I am not able to figure out the problem with this. Any help would be appreciated.
import { useRef } from "react";
import "./login.css";
import { loginCall } from "../../apicalls";
import { useContext } from "react";
import {AuthContext} from '../../context/AuthContext.js'
export default function Login() {
const email = useRef();
const password = useRef();
const [user,dispatch] = useContext(AuthContext);
const handleClick = (e) => {
e.preventDefault();
// loginCall(
// { email: email.current.value, password: password.current.value },
// dispatch
// );
console.log('hi');
};
// console.log(user);
return (
<div className="login">
<div className="loginWrapper">
<div className="loginLeft">
<h3 className="loginLogo">SocioFolio</h3>
<span className="loginDesc">
Connect with friends and the world around you on SocioFolio.
</span>
</div>
<div className="loginRight">
<form action=""
onSubmit={handleClick}
>
<div className="loginBox">
<input placeholder="Email" type="email" required className="loginInput" />
<input placeholder="Password" type="password" required minLength="6" className="loginInput" />
<button className="loginButton">Log In</button>
<span className="loginForgot">Forgot Password?</span>
<button className="loginRegisterButton">
Create a New Account
</button>
</div>
</form>
</div>
</div>
</div>
);
}

Related

I am having problems with setState

I'm working on a simple login site and to navigate between Login and Sign up site I made a state called "aktivSide" meaning "activeSite" which is supposed to determine whether the login site or the sign up site will display.
Whenever I click on the "Registrer Bruker" (sign up) button, I get this error message:
Uncaught TypeError: Cannot read properties of undefined (reading 'setState')
import planB from "./plan_b.json"
import logo from './bilder/logo.png'
import './App.css';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faEyeSlash, faEye } from '#fortawesome/free-solid-svg-icons'
import React from 'react';
class App extends React.Component {
//React
constructor(props) {
super(props);
this.state = {
aktivSide: "tom"
};
}
render() {
//JavaScript
return (
//jsx (javascript og HTML)
<>
<div onLoad={() => this.setState({ aktivSide: <LoggInn /> })} className="App">
<title>SpeedType</title>
<header className="App-header">
<div id="topp">
<img id="logoen" width={300} src={logo}></img>
</div>
<button onClick={() => console.log(this.state.aktivSide)}>state</button>
{this.state.aktivSide}
</header>
</div>
</>
)
function RegistrerBruker() {
return (
<>
<h1>Registrer Bruker</h1>
<input type="text" placeholder='brukernavn' className='input_text'></input>
<div className='passord'>
<input type="password" placeholder='passord' className='input_text'></input>
<FontAwesomeIcon className='eye_icon' id='eye_icon1' />
</div>
<p id='passordTekst'>tom</p>
<div className='knapper'>
<button className='knapper_login'><p>Logg Inn</p></button>
<button className='knapper_login'><p>Registrer deg</p></button>
</div>
</>
)
}
function LoggInn() {
//JavaScript
return (
//jsx
<>
<h1>Logg inn</h1>
<input type="text" placeholder='brukernavn' className='input_text'></input>
<div className='passord'>
<input type="password" placeholder='passord' className='input_text'></input>
<FontAwesomeIcon className='eye_icon' id='eye_icon1' />
</div>
<p id='passordTekst'>tom</p>
<div className='knapper'>
<button className='knapper_login'><p>Logg Inn</p></button>
<button onClick={() => this.setState({ aktivSide: <RegistrerBruker /> })} className='knapper_login'><p>Registrer deg</p></button>
</div>
</>
)
}
}
}
export default App;
I've tried adding:
this.aktivSide.bind(this);
to the constructor, but nothing will display.

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>

How to get form data as a object in reactjs

I'm trying to create a google form with react,
I have been creating all the questions as a components
if (props.type == "text") {
return (
<div className="box">
<h3 className="">{props.qustion}</h3>
<input className="short-text" placeholder="Your answer" id={"text"+props.id} name={"q"+props.id} type="text" onChange={updateField}/>
</div>
)
}
else if (props.type == "choice") {
return (
<div className="box">
<h3 className="">{props.qustion}{props.requre? <label className="requir">*</label>:""}</h3>
{props.answer.map(ans=>(
<div key={ans}>
<input className="radio" type="radio" id={ans} name={"radio"+props.id} value={ans} required={props.requre} onChange={updateField}/>
<label htmlFor={ans}>{ans}</label>
</div>
))
}
</div>
)
and I have been creating a form on the app file and put the components inside him,
return (
<div className="App">
<FormTitle/>
<form>
{
error? <h1>the sorce not found</h1>:data.map((item) =>(<Qustion qustion={item.question} type={item.type} requre={item.requre} id={item.id} answer={item.answares} key={item.id} />))
}
<div className="submit-right">
<input className="submit-button" type="submit" value="Submit" />
</div>
</form>
</div>
);
how to get all the form data as an object to create a post request ??
Try this function at start of the file where the form is
const formSubmit = (event) => {
event.preventDefault();
var data = new FormData(event.target);
let formObject = Object.fromEntries(data.entries());
console.log(formObject);
}
and in the form use this onSubmit={formSubmit}
<form onSubmit={formSubmit}>
<any element or components>
</form>
entries is not a function you can just reach it
const formSubmit = (event) => {
event.preventDefault();
var data = new FormData(event.target);
let formObject = Object.fromEntries(data.entries);
console.log(formObject);
}

react Js - × TypeError: Cannot read properties of undefined (reading 'params'

Here im trying to edit a post on my web app. But when I it says ×
TypeError: Cannot read properties of undefined (reading 'params'
can someone point out the error. im doing this wacthing a tutorial. It works fine. but in my code it doesnt work. and I tried importing useParams and using it. but it says useParams cannot use in class components.
can someone point out the error or give me a fix please?
import React, {Component} from 'react';
import axios from 'axios';
import {Dropdown} from "react-bootstrap";
import {useParams} from "react-router-dom";
class Feed2 extends Component {
constructor(props){
super(props);
this.state={
vehicle:"",
plateNo:"",
owner:"",
manufacturer:"",
manufacturedYear:"",
color:""
}
}
takInput = (e) => {
const {name,value}= e.target;
this.setState({
...this.state,
[name]:value
})
}
register = (e) =>{
e.preventDefault();
const id = this.props.match.params.id;
const {plateNo,owner,manufacturer,manufacturedYear,color,vehicle} = this.state;
const data ={
plateNo:plateNo,
owner:owner,
manufacturer:manufacturer,
manufacturedYear:manufacturedYear,
color:color,
vehicle:vehicle
}
console.log(data);
axios.put(`http://localhost:8080/registrations/update/${id}`,data).then((res)=>{
if(res.data.success){
alert("Post Updated");
this.setState({
vehicle:"",
plateNo:"",
owner:"",
manufacturer:"",
manufacturedYear:"",
color:""
})
}
})
}
componentDidMount(){
const id = this.props.match.params.id;
axios.get(`/registrations/${id}`).then((res) =>{
if(res.data.success){
this.setState({
plateNo:res.data.registrations.plateNo,
owner:res.data.registrations.owner,
manufacturer:res.data.registrations.manufacturer,
manufacturedYear:res.data.registrations.manufacturedYear,
color:res.data.registration.color,
vehicle:res.data.registrations.vehicle
})
console.log(this.state.registrations)
}
})
}
render() {
return (
<div className="container">
<div className="register-box">
<div className="Top">
<div className="box">
<input placeholder="Enter the licence plate number "
className="input2" name="plateNo" value={this.state.plateNo} onChange={this.takInput} />
</div>
</div>
<div className="box">
<input placeholder="Enter the owners name "
className="input2" name="owner" value={this.state.owner} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder=" Manufactured Year "
className="input2" name="manufacturedYear" value={this.state.manufacturedYear} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Manufacturer"
className="input2" name="manufacturer" value={this.state.manufacturer} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Color of the vehicle "
className="input2" name="color" value={this.state.color} onChange={this.takInput} />
</div>
<div className="box">
<input placeholder="Vehicle name "
className="input2" name="vehicle" value={this.state.vehicle} onChange={this.takInput} />
</div>
<Dropdown className="drop">
<Dropdown.Toggle variant="success" id="dropdown-basic">
Select Vehicle Type
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">Car</Dropdown.Item>
<Dropdown.Item href="#/action-2">Van</Dropdown.Item>
<Dropdown.Item href="#/action-3">Bus</Dropdown.Item>
<Dropdown.Item href="#/action-3">Lorry</Dropdown.Item>
<Dropdown.Item href="#/action-3">Three-weeler</Dropdown.Item>
<Dropdown.Item href="#/action-3">Bike</Dropdown.Item>
<Dropdown.Item href="#/action-3">Other</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
<div className="regbuttonContainer">
<button className="Button" onClick={this.register}>Register Vehicle</button>
</div>
</div>
</div>
);
}
}
export default Feed2;
I guess you need to wrap this component with withRouter HOC,
So it be like
export default withRouter(Feed2);
and import withRouter from 'react-router-dom';

ReduxForm handleSubmit refreshes page with fields assigned

Environment
ReduxForm: v6.5.0
Node: v8.1.2
Browser: Google Chrome
I've gone through all the existing issues on handleSubmit page refreshing, but none of them seems to solve my problem.
LoginForm
import React, { Component } from 'react'
import { reduxForm, Field, propTypes } from 'redux-form'
import classNames from 'classnames'
import loginValidation from './validation'
#reduxForm({
form: 'loginForm',
validate: loginValidation
})
export default class LoginForm extends Component {
static propTypes = {
...propTypes
}
inputField = ({ input, label, type, meta: { touched, error } }) => (
<fieldset className="form__fieldset login-form__fieldset">
<div className="form__field">
<input {...input}
type={type}
placeholder={touched && error ? error : label}
className={classNames('form__input login-form__input',
touched && error ? 'ng-invalid' : ''
)}
/> {/* .ng-invalid */}
</div>
{type === 'password' &&
<div className="form__helper login-form__helper">
<div className="small">
<a>I've forgotten my password</a>
</div>
</div>
}
</fieldset>
)
render() {
const { inputField, props } = this
const { handleSubmit, submitting } = props
return (
<div className="habbo-login-form">
<form className="login-form__form" onSubmit={handleSubmit}>
<Field name="username" type="text" component={inputField} label="Username" />
<Field name="password" type="password" component={inputField} label="Password" />
<button className="login-form__button" type="submit" disabled={submitting}>Let's go!</button>
</form>
<div className="login-form__social">
<div className="habbo-facebook-connect" type="large">
<button className="facebook-connect">Login with Facebook</button>
</div>
<div className="habbo-facebook-connect" type="small">
<button className="facebook-connect"></button>
</div>
</div>
</div>
)
}
}
And the HOC LoginHeader:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import classNames from 'classnames'
import LoginForm from './LoginForm'
import { authActions } from '../redux/modules/auth'
#connect(
state => ({
user: state.auth.user,
...state.form.loginForm
}),
{ ...authActions }
)
export default class LoginHeader extends Component {
static propTypes = {
user: PropTypes.object,
login: PropTypes.func.isRequired,
logout: PropTypes.func.isRequired
}
static contextTypes = {
router: PropTypes.object
}
onSubmit = (data) => this.props.login(data).then(console.log)
render() {
const { submitFailed } = this.props
const headerLoginForm = classNames(
'header__login-form',
submitFailed ? 'animated shake' : ''
)
return (
<div className="header__top sticky-header sticky-header--top">
<div className="wrapper">
<div className="header__top__content">
<div className={headerLoginForm}>
<LoginForm onSubmit={this.onSubmit} />
</div>
</div>
</div>
</div>
)
}
}
Even when I try to replace <form onSubmit={handleSubmit}> with <form onSubmit={(e) => e.preventDefault()}> my page still refreshes.
I'm unsure whether or not this is a problem with my coding, browser, version, or whatever else because practically yesterday it worked without any problem.

Resources