79660961

Date: 2025-06-10 18:17:18
Score: 2.5
Natty:
Report link

Just a small suggestion. Would you like to try writing it like this? It might help.

export const useRoomStore = defineStore('roomStore', () => {
  const body = ref<IRoomBody>(ROOM_BODY_DEFAULT)

  const { data, execute } = useFetch('/api/rooms', {
    key: 'rooms',
    method: 'post',
    body,
    immediate: false,
    onRequest ({ options }) {
      let bearer = 'Bearer '
      const token = localStorage.getItem('access_token')

      if (!token) {
        console.log('token ERROR GO TO LOGIN', token)
      } else {
        bearer = bearer + token
      }

      const headers = new Headers(options.headers)
      headers.set('Content-Type', 'application/json')
      headers.set('Authorization', bearer)

      options.headers = headers
    },
  })

  const fetch = () => execute()

  return {
    body,
    data,
    fetch,
  }
})

And if you’re interested in learning more about how to use $fetch, useFetch, and useAsyncData, this video might be helpful.

https://www.youtube.com/watch?v=njsGVmcWviY

Reasons:
  • Blacklisted phrase (1): youtube.com
  • Blacklisted phrase (1): this video
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Alex Liu