nuxt-community / axios-module

Secure and easy axios integration for Nuxt 2
https://axios.nuxtjs.org
MIT License
1.2k stars 245 forks source link

Why is `response.data.constructor !== Object` #565

Open LucidityDesign opened 2 years ago

LucidityDesign commented 2 years ago

anyobject.constructor === Object is a popular method to check if a variable is an object (e.g. see here: https://stackoverflow.com/a/16608074/1301058)

For the server-side response data from $axios.get this doesn't work because response.data.constructor === Object returns false.

Steps to reproduce:

  1. git clone git@github.com:LucidityDesign/axios-module.git
  2. yarn install
  3. yarn dev
  4. open localhost:3000/ssr
  5. console logs response type false
  6. see https://github.com/LucidityDesign/axios-module/blob/main/test/fixture/pages/ssr.vue#L17

Expected behaviour:

response.data.constructor === Object
// => true
LucidityDesign commented 2 years ago

So I tried to debug this phenomenon: After axios is transforming the response response.data.constructor === Object is true. Due to the resolving of promises I lose track of the variable, but as soon as they appear in this module (e.g. here) response.data.constructor === Object is false.