0

i am getting Unhandled Runtime Error Error: Request failed with status code 500

when i console.log(gotid ) i am getting id
but when i pass it to axios in network tab it becomes https://ask-over.herokuapp.com/questone/undefined

const [Item, setItem] = useState([]);
  const router = useRouter();
  var id = router.query.itmid;
  var gotid = id;
  console.log(gotid);

  useEffect(() => {
    axios
      .get("https://ask-over.herokuapp.com/questone/" + gotid)
      .then((result) => {
        console.log(result);
        // document.title = result.data[0].Name;
        setItem(result.data);
      });
    //  console.table(this.state.items);
  }, [id]);


1

2 Answers 2

1

yes just do this in useEffect:

useEffect(() => {
   if(id != null){
      axios.get(url +"/"+id).then(...)
   }
},[id])
Sign up to request clarification or add additional context in comments.

Comments

0

No need to create extra variables :


const [Item, setItem] = useState([]);
  const router = useRouter();
  var id = router.query.itmid;
  console.log(id);

  useEffect(() => {
    axios
      .get("https://ask-over.herokuapp.com/questone/" + id)
      .then((result) => {
        console.log(result);
        // document.title = result.data[0].Name;
        setItem(result.data);
      });
    //  console.table(this.state.items);
  }, [id]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.