next.js 用于获取具有事件处理程序的API的动态路由

rm5edbpk  于 2023-11-18  发布在  其他
关注(0)|答案(2)|浏览(106)

我试图创建一个天气应用程序https://havadurumu-7pqg.vercel.app/,并找出如何创建一个动态路线的输入值的搜索表单与onSubmit或onClick。

│  pages
│   ├── _app.js
│   ├── _document.js
│   ├── hava
│   │   └── [city].jsx
│   └── index.js
│

字符串
index.js文件(缩短):

export default function Home() {

  const [city, setCity] = useState('')
  const [weather, setWeather] = useState([]);
  const [loading, setLoading] = useState(false);
  const router = useRouter();

  const fetchWeather = async (e) => {
    e.preventDefault();
    const axiosRequest = require('axios');
    try {
      const url = `https://api.openweathermap.org/geo/1.0/direct?q=${city}&&appid=${process.env.NEXT_PUBLIC_WEATHER_KEY}`;
      const response = await axiosRequest.get(url);
      const url2 = `https://api.openweathermap.org/data/2.5/forecast/daily?lat=${response.data[0].lat}&lon=${response.data[0].lon}&units=metric&cnt=16&appid=${process.env.NEXT_PUBLIC_WEATHER_KEY}`;
      const response2 = await axiosRequest.get(url2);
      router.push(`/hava/${city}`);
      setWeather(response2);
      // useEffect(() => {
      //   console.log(weather.data)
      // },[weather]);

    } catch (error) {
      console.error(error)
    }

  }

  if (loading) {
    return <Spinner/>
  } else {

    return (

        <form onSubmit={fetchWeather}
                    className={'flex justify-between w-full items-center m-auto p-1 bg-transparent border-2 border-gray-400 text-white rounded-2xl'}>
                <div>
                  <input
                      onChange={(e) => setCity(e.target.value)}
                      className={'bg-transparent border-none text-gray-600 focus:outline-none text-sm'}
                      type="text"
                      placeholder="Search city"/>
                </div>
                <button type="submit">
                  <BsSearch size={20}/>
                </button>
              </form>
            </div>
            {/*Weather*/}
            <div className='lg:mx-auto md:mx-auto  overflow-x-auto justify-around container'>
              {weather.data && <Weather data={weather}/>}
            </div>

              
    );
  }


[city].jsx:

const Weather = ({data}) => {
    // const router = useRouter();
    // const {city} = router.query;
    // console.log(data)

    let dates =[];
    let tempday = [];
    let tempnight = [];
    let conditions = [];
    let description = [];
    let aciklama = []

    try {
         if (data && data.data && data.data.list){
            for (let i = 0; i < 16; i++) {
                const dt = data.data.list[i].dt;
                const options = {month: 'short', weekday: 'short', day: 'numeric'};
                const date = new Date(dt * 1000).toLocaleDateString('tr-TR', options);
                dates.push(date);
                console.log(data.data.list[i].temp.max)
            }


                for (let i = 0; i < 16; i++) {
                    const temp = Math.trunc(data.data.list[i].temp.max);
                    tempday.push(temp);
                }

                for (let i = 0; i < 16; i++) {
                    const temp = Math.trunc(data.data.list[i].temp.min);
                    tempnight.push(temp);
                }

                for (let i = 0; i < 16; i++) {
                    const condition = data.data.list[i].weather[0].main;
                    conditions.push(condition);
                }

                const city = data.data.city.name;

            for (let i = 0; i < 16; i++) {
                const desc = data.data.list[i].weather[0].description;
                description.push(desc)
            }

            const aciklama = description.map((desc) => {
                switch (desc) {
                    case "overcast clouds":
                        return "cok bulutlu";
                    case "broken clouds":
                        return "cok bulutlu";
                    case "scattered clouds":
                        return "az bulutlu";
                    case "few clouds":
                        return "az bulutlu";
                    case "heavy intensity rain":
                        return "çok yağmur";
                    case "moderate rain":
                        return "yağmurlu";
                    case "light rain":
                        return "az yağmur";
                    case "sky is clear":
                        return "güneşli";
                    default:
                        return desc;
                }
            });
            

    } catch (error) {
        console.error(error)
    }

    return ( 
        <div className="flex">
                            <div className='w-[95px]'>
                                <a className="text-gray-50 text-xs font-bold hover:underline">{dates[0]}</a>
                            </div>
                            <div className="max-w-[24px]">
                                {description[0] === "sky is clear" &&
                                    <Image src={sun} alt='sun-icon'/> || description[0] === "light rain" &&
                                    <Image src={lightrain}
                                           alt='rain-cloud-sun-icon'/> || description[0] === "moderate rain" &&
                                    <Image src={moderaterain}
                                           alt='rain-cloud-icon'/> || description[0] === "heavy intensity rain" &&
                                    <Image src={heavyrain}
                                           alt='rain-cloud-icon'/> || description[0] === "overcast clouds" &&
                                    <Image src={cloud} alt="cloud-sun"/> || description[0] === "broken clouds" &&
                                    <Image src={midcloud} alt="cloud-sun"/> || description[0] === "scattered clouds" &&
                                    <Image src={fewcloud} alt="sun"/> || description[0] === "few clouds" &&
                                    <Image src={fewcloud} alt="sun-cloud-icon"/> || description[0] === "Snow" &&
                                    <Image className="w-3/4" src={snow} alt="snow-icon"/>}
                            </div>
                            <div className='pl-2'>
                                    <span
                                        className="p-0.5 text-xs font-normal uppercase tracking-wider text-gray-800 bg-gray-200 rounded-lg bg-opacity-50">{aciklama[0]}
                                    </span>
                            </div>
                        </div>
)}

export default Weather


版本

├── [email protected]
├── [email protected]
├── [email protected]

console.log(response2)

"data": {
    "city": {
        "id": 750269,
        "name": "Bursa",
        "coord": {
            "lon": 29.0675,
            "lat": 40.1827
        },
    },
    "list": [ 
        {// the first day of the 16 days of weather forecast
            "dt": 1699174800,
            "sunrise": 1699159023,
            "sunset": 1699196252,
            "temp": {
                "day": 23.31,
                "min": 16.18,
                "max": 24.06,
                "night": 18.54,
                "eve": 22,
                "morn": 16.39
            },
            
            "pressure": 1010,
            "humidity": 44,
            "weather": [
                        {
                "id": 501,
                "main": "Rain",
                "description": "moderate rain",
                "icon": "10d"
            }
            ],


当提交的形式通过搜索城市在着陆页上得到了一个
“TypeError:无法读取Weather([city].jsx:30:33)中未定义的属性(阅读”data“)”
下面是错误行“[city].jsx:30:33”:
const dt = data.data.list[i].dt;
chatgpt建议的可能原因:
·数据正在异步获取,并且在检索数据时存在延迟
·确保正确定义天气组件以接受天气属性
·确认天气组件正在接收天气状态作为 prop
看来我的天气.jsx文件是有麻烦访问数据对象,来自index.js文件.

xtupzzrd

xtupzzrd1#

我觉得你的if statement不对

if (!data || !data.data || !data.data.list){}

字符串
这是说

if `data` does not exist or 
    data.data does not exist or 
    data.data.list does not exist


执行if块内的代码。可能在那个时刻data.data还没有被创建。如果你改变if到:

if (data && data.data && data.data.list){}


你的代码应该能工作

hyrbngr7

hyrbngr72#

  • 解决 *

我只是把fetchWeather函数从index.js移到了[city].jsx文件中。然后用useState钩子获取[city].jsx文件中的天气,如下所示:

for (let i = 0; i < 16; i++) {
                const temp = Math.trunc(weather.list[i].temp.max);
                tempday.push(temp);
            }

字符串

相关问题