简体   繁体   English

TypeError:无法读取未定义的属性(读取“地图”)NEXTJS

[英]TypeError: Cannot read properties of undefined (reading 'map') NEXTJS

I am trying to fetch an image from this API by using getStaticProps but I can't seem to get it.我正在尝试使用 getStaticProps 从此 API 获取图像,但我似乎无法获得它。

If I add a '?'如果我添加一个“?” to the code like this then in the log it says 'undefined'.像这样的代码,然后在日志中显示“未定义”。 Why can't I fetch it?为什么我拿不到?

<div>
  {hotelPics.map((hotelpic)=> (
  <Image src={hotelpic.url} />
  ))}
</div>

this is the code这是代码

import Image from 'next/image';
import React from 'react'


function Images({hotelPics }) {
 
    console.log(hotelPics);
    
    return (
        <div>
    {hotelPics.map((hotelpic)=> (
        <Image src={hotelpic.url}/>
    ))}    
        </div>
    )
}

export async function getStaticProps() {
    const res= await fetch ('https://obmng.dbm.guestline.net/api/hotels?collection-id=OBMNG')
    const hotelPics = await res.json()

return {
    props: {
        hotelPics,
    },
}

}

export default Images

getStaticProps are "compiling" at build time, so you need to check isFallback variable getStaticProps在构建时“编译”,因此您需要检查isFallback变量

Also, you have nested images array in hotelPics array returned from getStaticProps此外,您在从getStaticProps返回的hotelPics数组中嵌套了images数组

import Image from 'next/image';
import { useRouter } from 'next/router';
import React from 'react'


function Images({hotelPics}) {
  const router = useRouter();
  if (router.isFallback) {
    return <h1>Loading...</h1>;
  }

    //console.log(hotelPics.images)
    return (
        <div>
        {hotelPics.map((hotelpic)=> (
            hotelpic.images.map((image) => (
              //replace here with Image tag
              <p key={image.url}>{image.url}</p>
              )
            )
        ))}    
        </div>
    )

    
}

export async function getStaticProps() {
    const res= await fetch ('https://obmng.dbm.guestline.net/api/hotels?collection-id=OBMNG')
    const hotelPics = await res.json()

return {
    props: {
        hotelPics,
    },
}

}

export default Images

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Reactjs(Nextjs):无法读取未定义的属性(读取“地图”)Reactjs(Nextjs) - Reactjs(Nextjs): Cannot read properties of undefined (reading 'map') Reactjs(Nextjs) TypeError:无法读取未定义的属性(读取“地图”) - State 返回未定义 - TypeError: Cannot read properties of undefined (reading 'map') - State returns undefined TypeError:无法读取未定义的属性(正在读取“映射”)(JavaScript 数组映射) - TypeError: Cannot read properties of undefined (reading 'map') (JavaScript array map) TypeError:无法读取未定义 NextJS 的属性“地图” - TypeError: Cannot read property 'map' of undefined NextJS 类型错误:无法读取未定义的属性(读取“地图”)Table.render - TypeError: Cannot read properties of undefined (reading 'map') Table.render 反应:类型错误:无法读取未定义的属性(读取“地图”) - React: TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Uncaught TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React 错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') TypeError:无法读取未定义的属性(读取“地图”)React JS - TypeError: Cannot read properties of undefined (reading 'map') React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM