Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A Cannot read properties of undefined (reading 'map') error in MERN stack project

In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the...

0 answers  ·  posted 20d ago by emmaluga‭  ·  edited 19d ago by Alexei‭

Question reactjs react-hooks
#5: Nominated for promotion by user avatar Alexei‭ · 2024-11-03T07:35:18Z (18 days ago)
#4: Post edited by user avatar Alexei‭ · 2024-11-02T07:33:40Z (19 days ago)
minor title fix
  • Cannot read properties of undefined (reading 'map') error in mern stack project
  • Cannot read properties of undefined (reading 'map') error in MERN stack project
#3: Post edited by user avatar Alexei‭ · 2024-11-02T07:31:23Z (19 days ago)
highlighted the code + added relevant tag
  • In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the console, I get a fulfilled promise.
  • I console.log the setState function to see what data is logged there but it returned undefined. why is my setState undefined?
  • In my backend, data displays fine when I test on thunder client in vscode. I also paste http://localhost:8080/data' in chrome and it also works.
  • why is data undefined?
  • //parent component
  • const Shopstore = () => {
  • const [dataProducts, setData ] = useState([])
  • const url = 'http://localhost:8080/data'
  • useEffect(()=> {
  • const fetchfunc = async ()=> {
  • try {
  • const response = await fetch(url)
  • if(!response.ok){
  • throw new Error("network issues pls wait ");
  • }
  • const data = await response.json()
  • //this console.log returns a fulfilled promise
  • console.log(data)
  • //this console.log part returns undefined
  • console.log( setData( data.dataProducts ));
  • } catch (error) {
  • console.log('error', error)
  • }
  • }
  • fetchfunc()
  • }, [] )
  • return (
  • <div className='Shopstore mt-5'>
  • <Container>
  • <Row>
  • <Col md={10} className='big mt-3'>
  • { dataProducts ? dataProducts.map((product)=> {
  • return(
  • <Listings key={product._id } { ... product } />
  • )
  • }) : <p>no data</p> }
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Shopstore
  • //child component
  • const Listings = (products) => {
  • const { image, heading, price, _id } = products
  • return (
  • <div className='listings mt-3 mb-3'>
  • <Container>
  • <Row>
  • <Col md={4} id = { _id }>
  • <div className="card mx-auto" style={{width: '250px'}}>
  • <Card>
  • <div className='image mx-auto '>
  • <Link to={ `/shop/${products._id}` }> <img src={image} alt="" className='img-fluid' style={{width: '300px'}} /> </Link>
  • <CardBody>
  • <div className='title'>
  • <h5>{heading}</h5>
  • </div>
  • <div className='price'>
  • <span>{price}</span>
  • </div>
  • <div className=' mx-auto' >
  • <button className='mx-auto cartbutton' >Add to cart</button>
  • </div>
  • </CardBody>
  • </div>
  • </Card>
  • </div>
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Listings
  • The ternary operator triggers no data as there is no data to display.please help recreate in your machine. thanks.
  • In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the console, I get a fulfilled promise.
  • I console.log the setState function to see what data is logged there but it returned undefined. why is my setState undefined?
  • In my backend, data displays fine when I test on thunder client in vscode. I also paste http://localhost:8080/data' in chrome and it also works.
  • why is data undefined?
  • ```react
  • //parent component
  • const Shopstore = () => {
  • const [dataProducts, setData ] = useState([])
  • const url = 'http://localhost:8080/data'
  • useEffect(()=> {
  • const fetchfunc = async ()=> {
  • try {
  • const response = await fetch(url)
  • if(!response.ok){
  • throw new Error("network issues pls wait ");
  • }
  • const data = await response.json()
  • //this console.log returns a fulfilled promise
  • console.log(data)
  • //this console.log part returns undefined
  • console.log( setData( data.dataProducts ));
  • } catch (error) {
  • console.log('error', error)
  • }
  • }
  • fetchfunc()
  • }, [] )
  • return (
  • <div className='Shopstore mt-5'>
  • <Container>
  • <Row>
  • <Col md={10} className='big mt-3'>
  • { dataProducts ? dataProducts.map((product)=> {
  • return(
  • <Listings key={product._id } { ... product } />
  • )
  • }) : <p>no data</p> }
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Shopstore
  • //child component
  • const Listings = (products) => {
  • const { image, heading, price, _id } = products
  • return (
  • <div className='listings mt-3 mb-3'>
  • <Container>
  • <Row>
  • <Col md={4} id = { _id }>
  • <div className="card mx-auto" style={{width: '250px'}}>
  • <Card>
  • <div className='image mx-auto '>
  • <Link to={ `/shop/${products._id}` }> <img src={image} alt="" className='img-fluid' style={{width: '300px'}} /> </Link>
  • <CardBody>
  • <div className='title'>
  • <h5>{heading}</h5>
  • </div>
  • <div className='price'>
  • <span>{price}</span>
  • </div>
  • <div className=' mx-auto' >
  • <button className='mx-auto cartbutton' >Add to cart</button>
  • </div>
  • </CardBody>
  • </div>
  • </Card>
  • </div>
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Listings
  • ```
  • The ternary operator triggers no data as there is no data to display.
#2: Post edited by user avatar emmaluga‭ · 2024-11-01T21:11:11Z (20 days ago)
  • In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the console, I get a fulfilled promise.
  • I console.log the setState function to see what data is logged there but it returned undefined. why is my setState undefined?
  • In my backend, data displays fine when I test on thunder client in vscode. I also paste http://localhost:8080/data' in chrome and it also works.
  • why is data undefined?
  • //parent component
  • const Shopstore = () => {
  • // const { dataProducts } = useProductsQuery()
  • const [dataProducts, setData ] = useState([])
  • const url = 'http://localhost:8080/data'
  • useEffect(()=> {
  • const fetchfunc = async ()=> {
  • try {
  • const response = await fetch(url)
  • if(!response.ok){
  • throw new Error("network issues pls wait ");
  • }
  • const data = await response.json()
  • //this console.log returns a fulfilled promise
  • console.log(data)
  • //this console.log part returns undefined
  • console.log( setData( data.dataProducts ));
  • } catch (error) {
  • console.log('error', error)
  • }
  • }
  • fetchfunc()
  • }, [] )
  • return (
  • <div className='Shopstore mt-5'>
  • <Container>
  • <Row>
  • <Col md={10} className='big mt-3'>
  • { dataProducts ? dataProducts.map((product)=> {
  • return(
  • <Listings key={product._id } { ... product } />
  • )
  • }) : <p>no data</p> }
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Shopstore
  • //child component
  • const Listings = (products) => {
  • const { image, heading, price, _id } = products
  • return (
  • <div className='listings mt-3 mb-3'>
  • <Container>
  • <Row>
  • <Col md={4} id = { _id }>
  • <div className="card mx-auto" style={{width: '250px'}}>
  • <Card>
  • <div className='image mx-auto '>
  • <Link to={ `/shop/${products._id}` }> <img src={image} alt="" className='img-fluid' style={{width: '300px'}} /> </Link>
  • <CardBody>
  • <div className='title'>
  • <h5>{heading}</h5>
  • </div>
  • <div className='price'>
  • <span>{price}</span>
  • </div>
  • <div className=' mx-auto' >
  • <button className='mx-auto cartbutton' >Add to cart</button>
  • </div>
  • </CardBody>
  • </div>
  • </Card>
  • </div>
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Listings
  • The ternary operator triggers no data as there is no data to display.please help recreate in your machine. thanks.
  • In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the console, I get a fulfilled promise.
  • I console.log the setState function to see what data is logged there but it returned undefined. why is my setState undefined?
  • In my backend, data displays fine when I test on thunder client in vscode. I also paste http://localhost:8080/data' in chrome and it also works.
  • why is data undefined?
  • //parent component
  • const Shopstore = () => {
  • const [dataProducts, setData ] = useState([])
  • const url = 'http://localhost:8080/data'
  • useEffect(()=> {
  • const fetchfunc = async ()=> {
  • try {
  • const response = await fetch(url)
  • if(!response.ok){
  • throw new Error("network issues pls wait ");
  • }
  • const data = await response.json()
  • //this console.log returns a fulfilled promise
  • console.log(data)
  • //this console.log part returns undefined
  • console.log( setData( data.dataProducts ));
  • } catch (error) {
  • console.log('error', error)
  • }
  • }
  • fetchfunc()
  • }, [] )
  • return (
  • <div className='Shopstore mt-5'>
  • <Container>
  • <Row>
  • <Col md={10} className='big mt-3'>
  • { dataProducts ? dataProducts.map((product)=> {
  • return(
  • <Listings key={product._id } { ... product } />
  • )
  • }) : <p>no data</p> }
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Shopstore
  • //child component
  • const Listings = (products) => {
  • const { image, heading, price, _id } = products
  • return (
  • <div className='listings mt-3 mb-3'>
  • <Container>
  • <Row>
  • <Col md={4} id = { _id }>
  • <div className="card mx-auto" style={{width: '250px'}}>
  • <Card>
  • <div className='image mx-auto '>
  • <Link to={ `/shop/${products._id}` }> <img src={image} alt="" className='img-fluid' style={{width: '300px'}} /> </Link>
  • <CardBody>
  • <div className='title'>
  • <h5>{heading}</h5>
  • </div>
  • <div className='price'>
  • <span>{price}</span>
  • </div>
  • <div className=' mx-auto' >
  • <button className='mx-auto cartbutton' >Add to cart</button>
  • </div>
  • </CardBody>
  • </div>
  • </Card>
  • </div>
  • </Col>
  • </Row>
  • </Container>
  • </div>
  • )
  • }
  • export default Listings
  • The ternary operator triggers no data as there is no data to display.please help recreate in your machine. thanks.
#1: Initial revision by user avatar emmaluga‭ · 2024-11-01T21:03:45Z (20 days ago)
Cannot read properties of undefined (reading 'map') error in mern stack project
In my project, I try to fetch data from the backend to display in the frontend using fetch API. Each time I do this I get Cannot read properties of undefined (reading 'map') error. When I check the console, I get a fulfilled promise.

I console.log the setState function to see what data is logged there but it returned undefined. why is my setState undefined? 

In my backend, data displays fine when I test on thunder client in vscode. I also paste http://localhost:8080/data' in chrome and it also works.

why is data undefined?


//parent component
const Shopstore =  ()  => {

//  const { dataProducts } = useProductsQuery()
 const [dataProducts, setData ] = useState([])
 
  const url = 'http://localhost:8080/data'


  useEffect(()=> {

  const fetchfunc = async ()=> {
     try {
      const response = await fetch(url)
   
      if(!response.ok){
         throw new Error("network issues pls wait ");  
      }
      const data = await response.json()
       //this console.log returns a fulfilled promise
       console.log(data)

    //this console.log part returns undefined
     console.log( setData( data.dataProducts ));
       


     } catch (error) {
      console.log('error', error)
     }
  }
    

   fetchfunc()

  }, [] )

  


  return (
   
      <div className='Shopstore mt-5'>

      <Container>
      <Row>
     

      <Col md={10} className='big mt-3'>
          
       
          
           { dataProducts ? dataProducts.map((product)=> {
               return(
               <Listings key={product._id } { ... product } />
             
               )
           })  : <p>no data</p> }

          
            
          </Col>
      </Row>
     </Container>
      </div>
  )
}

export default Shopstore



//child component
const Listings = (products) => {

    const {  image, heading, price, _id  } = products

  return (

    <div className='listings mt-3 mb-3'>
        <Container>
         <Row>
         <Col md={4} id = { _id }>
         <div className="card mx-auto" style={{width: '250px'}}>
         
             <Card>
            <div className='image mx-auto '>
           <Link to={ `/shop/${products._id}` }> <img src={image} alt="" className='img-fluid' style={{width: '300px'}} /> </Link>
           <CardBody>
            <div className='title'>
              <h5>{heading}</h5>
            </div>
            <div className='price'>
              <span>{price}</span>
            </div>

            <div className=' mx-auto' >
            <button className='mx-auto cartbutton'  >Add to cart</button>
             </div>

           </CardBody>
            </div>
          </Card>    
         </div>
         </Col>
         </Row>
        </Container>
      
    </div>
  )
}

export default Listings


The ternary operator triggers no data as there is no data to display.please help recreate in your machine. thanks.