yasuto2002 / SyncMemo

0 stars 0 forks source link

Nuxt3のfetchApiでステータスコードを使ってエラーハンドリング #42

Closed yasuto2002 closed 1 year ago

yasuto2002 commented 1 year ago

エラーコードを判定

<script setup lang="ts">

let errorStatus;
const { data: myData, error: error }: { data: any; error?: any } =
  await useFetch(
    "https://jsonplaceholder.typicode.com/404",
    {
      async onResponseError({ response }) { // onResponseError is a "ohmyfetch" method
        errorStatus = response.status; // assign the error to the declared variable "errorStatus"
      },
    }
  ); 

return { myData, error, errorStatus }; // returning all the variables

</script>

https://github.com/nuxt/nuxt/issues/12885