admin管理员组文章数量:1313801
I am getting the below error
AxiosError {message: 'Request failed with status code 404
The code is:
"use client"
import axios from "axios";
import { useEffect, useState } from "react";
const page = ({params}) => {
const [paramsObj, setparamsObj] = useState({})
const awaitParams = async ()=>{
const awaitedParams = await params;
setparamsObj(awaitedParams);
}
useEffect(() => {
awaitParams();
}, [])
const {id}= paramsObj;
console.log(id);
const getUser=async ()=>{
const response= await axios.get(`/${id}`);
console.log(response.data);
}
useEffect(() => {
getUser();
},[] )
return (
<div>page{id}</div>
)
}
export default page
I was expecting to get the user object corresponding to the id on my console, but it is showing the error. Though I replace the ${id} with any value like 1, then it gives the user object with id 1 on console. I am stuck and not getting answer anywhere.
I am getting the below error
AxiosError {message: 'Request failed with status code 404
The code is:
"use client"
import axios from "axios";
import { useEffect, useState } from "react";
const page = ({params}) => {
const [paramsObj, setparamsObj] = useState({})
const awaitParams = async ()=>{
const awaitedParams = await params;
setparamsObj(awaitedParams);
}
useEffect(() => {
awaitParams();
}, [])
const {id}= paramsObj;
console.log(id);
const getUser=async ()=>{
const response= await axios.get(`https://jsonplaceholder.typicode/users/${id}`);
console.log(response.data);
}
useEffect(() => {
getUser();
},[] )
return (
<div>page{id}</div>
)
}
export default page
I was expecting to get the user object corresponding to the id on my console, but it is showing the error. Though I replace the ${id} with any value like 1, then it gives the user object with id 1 on console. I am stuck and not getting answer anywhere.
Share Improve this question asked Feb 1 at 11:05 AshishAshish 11 Answer
Reset to default 0The reason of error is that id
is undefined at first. and because of that u face the error and no result.
First of all
u don't need to use useEffect
& useState
to get the query search params in page component in nextjs. instead of :
const page = ({params}) => {
const [paramsObj, setparamsObj] = useState({})
const awaitParams = async ()=>{
const awaitedParams = await params;
setparamsObj(awaitedParams);
}
useEffect(() => {
awaitParams();
}, [])
Do this :
const page = ({params}) => {
const awaitedParams = await params;
And then, move your fetch
function, into useEffect , like this :
useEffect(() => {
if (!id) return;
const getUser = async () => {
try {
const response = await axios.get(`https://jsonplaceholder.typicode/users/${id}`);
console.log(response.data);
} catch (error) {
console.error("Error fetching user:", error);
}
};
getUser();
}, [id]);
. It should work.
本文标签:
版权声明:本文标题:reactjs - Uncaught (in promise) AxiosError {message: 'Request failed with status code 404', name: & 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741878252a2402593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论