#Error handling when No error.

7 messages · Page 1 of 1 (latest)

olive palm
#

Im following a tutorial seems to be on a older version of Next.js this seems to work prior but now its throwing an error.
error is not defined
This is the code

export default function DeleteIcon({ id }) {
    const [isLoading, setIsLoading] = useState(false)
    const router = useRouter()
  
    const handleClick = async () => {
      setIsLoading(true)
      
      const res = await fetch(`http://localhost:3000/api/tickets/${id}`, {
        method: 'DELETE'
      })
      const json = await res.json()
  
      if (json.error) {
        console.log(error)
        setIsLoading(false)
      }
      if (!json.error) {
        router.refresh()
        router.push('/tickets')
      }
    }

What is the best way to resolve this issue or best practice.

burnt auroraBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

frigid sinew
#

Ideally you would first try and fix the error

#

but if thats not possible.. couldnt you just catch the error and render a not sucessfull message?

#

also send the json response if thats possible

olive palm
#
    const [isLoading, setIsLoading] = useState(false)
    const router = useRouter()
  
    const handleClick = async () => {
      setIsLoading(true)
      
      const res = await fetch(`http://localhost:3000/api/tickets/${id}`, {
        method: 'DELETE'
      })
      const json = await res.json()
  
      if (json.error) {
        console.log(.json.error) //< add json.
        setIsLoading(false)
      }
      if (!json.error) {
        router.refresh()
        router.push('/tickets')
      }
    }
burnt auroraBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1200994327430701077 message)