[英]TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - Why? What does it mean? How to solve it?
I was trying to use react useContext hook between search.js and result.js component but somehow it's not working.我试图在 search.js 和 result.js 组件之间使用react useContext挂钩,但不知何故它不起作用。
I want to send data from one component to another.我想将数据从一个组件发送到另一个组件。 using props is a bit complex so I decided to use context api.
使用道具有点复杂,所以我决定使用上下文 api。 I added value as well but why it's not iterable?
我也增加了价值,但为什么它不可迭代? It's reaching the result component but that destructing line of contextApi is making a big issue here.
它到达了结果组件,但是 contextApi 的破坏线在这里造成了一个大问题。
The error I am getting is TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))我得到的错误是TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
search.js code -> search.js 代码->
import React, { createContext, useState } from 'react';
import { useHistory, useParams } from 'react-router';
import './Search.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Link } from 'react-router-dom';
import vehicleData from '../Home/vehicleData.json';
export const PlaceContext = createContext();
const Search = () => {
const [searchPlace, setSearchPlace] = useState({
from: '',
to: ''
})
const [route, setRoute] = useState({
from: '',
to: ''
})
const {type} = useParams();
let ride;
vehicleData.map(vehicle => {
if(vehicle.Name === type) {
ride = vehicle.Name;
}
})
const handleBlur = (event => {
const newSearchPlace = {...searchPlace}
newSearchPlace[event.target.name] = event.target.value;
setSearchPlace(newSearchPlace);
})
const history = useHistory();
const resultPage = rideOption => {
const url = `/result/${rideOption}`;
history.push(url);
console.log("pacche");
}
const handleClick = (event) => {
if (searchPlace.from && searchPlace.to) {
setRoute(searchPlace);
console.log(searchPlace, " && ", route);
}
}
console.log(searchPlace);
return (
<PlaceContext.Provider value={[searchPlace, setSearchPlace]}>
<div className="d-flex flex-column searchField">
<div className="inputArea">
<h4>From</h4>
<input onBlur={handleBlur} name="from" id="fromField" type="text" placeholder="Type Your Starting Point" />
</div>
<div className="inputArea">
<h4>To</h4>
<input onBlur={handleBlur} name="to" id="toField" type="text" placeholder="Type Your Destination Point" />
</div>
<button onMouseEnter={handleClick} onClick={() => resultPage(ride)} className="checkBtn btn btn-success btn-outline-info btn-lg m-3">Check</button>
</div>
</PlaceContext.Provider>
);
};
export default Search;
and the result.js code ->和 result.js 代码->
import { useParams } from 'react-router';
import 'bootstrap/dist/css/bootstrap.min.css';
import './Result.css';
import vehicleData from '../Home/vehicleData.json';
import { PlaceContext } from '../Search/Search';
const Result = () => {
const [searchPlace, setSearchPlace] = useContext(PlaceContext);
console.log(searchPlace);
const {ride} = useParams();
let transportOption = ride;
let transportName, transportImg;
vehicleData.map(vehicle => {
if(vehicle.Name === transportOption) {
transportName = vehicle.Name;
transportImg = vehicle.imgUrl;
}
})
return (
<div className="d-flex justify-content-between showComponent">
<div className="placeAndPrice">
<div className="placeName">
<p>From : {}</p>
<p>To : {}</p>
</div>
<div className="d-flex justify-content-around pricePart">
<img src={transportImg}/>
<div className="d-flex flex-column priceBox">
<p>{transportName}</p>
<p>131 Taka</p>
</div>
</div>
</div>
<div className="mapBox">
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d7304.049644099905!2d90.3781836249269!3d23.746494238365205!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3755b8b0e848958f%3A0xd56e2e3180c6a3ce!2sKalabagan%2C%20Dhaka%201205!5e0!3m2!1sen!2sbd!4v1616325753856!5m2!1sen!2sbd" loading="lazy"></iframe>
</div>
</div>
);
};
export default Result;```
So what is the error and how to solve it?
The error comes when you try to loop over a non-iterable entity.当您尝试遍历不可迭代的实体时会出现错误。 In your case
vehicleData
seems to be that object ( non-iterable ) instead of an array ( iterable ).在您的情况下,
vehicleData
似乎是 object (不可迭代)而不是数组(可迭代)。
EDIT - I think the above would have given .map is not a function
.编辑- 我认为上面会给出
.map is not a function
。 One thing I noted is that you are not actually having any {children}
prop being used in the PlaceContext.Provider
so how are you actually able to do the following:-我注意到的一件事是,您实际上没有在
PlaceContext.Provider
中使用任何{children}
道具,那么您实际上如何能够执行以下操作:-
<Search>
<Result/>
</Search>
Ideally your PlaceContext.Provider
bit should have following type of implementation:-理想情况下,您的
PlaceContext.Provider
位应具有以下类型的实现:-
return (
<PlaceContext.Provider value={[searchPlace, setSearchPlace]}>
/* {Your own divs and bla bla} */
{children}
/* the above will come from Search prop */
</PlaceContext.Provider>
);
};
You can directly use Result
instead of children
too if you don't have many children
to consider.如果您没有太多要考虑的
children
,您也可以直接使用Result
而不是children
。
Please check whether you implementation is in line with what is described here - https://reactjs.org/docs/context.html#contextprovider请检查您的实现是否符合此处描述的内容 - https://reactjs.org/docs/context.html#contextprovider
When I see your code, you are importing vehicleData
from the json file (vehicleData.json) You may want to check if vehicleData
is imported correctly by console.log(vehicleData)
.当我看到您的代码时,您正在从 json 文件 (vehicleData.json) 导入
vehicleData
您可能需要检查是否通过console.log(vehicleData)
vehicleData
导入了 vehicleData。 If the data is imported as you expected, then please check if vehicleData
is an array type.如果数据按预期导入,请检查
vehicleData
是否为数组类型。 If not, you cannot use map function.如果没有,则不能使用 map function。 If your data is an object type, then you can use something like following:
如果您的数据是 object 类型,那么您可以使用如下内容:
for (const [key, value] of Object.entries(object)) {
console.log(`${key}: ${value}`);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.