nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

$http.get(...).text is not a function #86

Closed lcherone closed 4 years ago

lcherone commented 4 years ago

I'm getting the above error using code directly taken from the example in docs:

https://http.nuxtjs.org/guide/usage.html#using-in-asyncdata

 "dependencies": {
    "@nuxt/http": "^0.3.8",
<template>
  <pre>{{ ip }}</pre>
</template>

<script>
export default {
  async asyncData({ $http }) {
    const ip = await $http.get('http://icanhazip.com').text()
    return { ip }
  }
};
</script>

browser

screenshot-localhost_3000-2019 12 22-00_15_20

console

 ERROR  $http.get(...).text is not a function                                                      00:10:30

  at asyncData (about.vue:16:0)
  at promisify (.nuxt/utils.js:245:0)
  at Promise.all.Components.map.Component (.nuxt/server.js:215:0)
  at Array.map (<anonymous>)
  at module.exports../.nuxt/server.js.__webpack_exports__.default (.nuxt/server.js:210:0)
ghost commented 4 years ago

This issue as been imported as question since it does not respect http issue template. Only bug reports and feature requests stays open to reduce maintainers workload. If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically. Your question is available at https://cmty.app/nuxt/http/issues/c20.

lcherone commented 4 years ago

bot closed it, ill leave it with you..

lcherone commented 4 years ago

additionally, the response object looks like:

Response 
  body: ReadableStream
    locked: false
    __proto__: ReadableStream
  bodyUsed: false
  headers: Headers
    __proto__: Headers
  ok: true
  redirected: false
  status: 200
  statusText: "OK"
  type: "cors"
  url: "http://icanhazip.com/"
  __proto__: Response

network all good and shows content (ip).. also the same error if done outside of asyncData as a normal method and called in mounted.. maybe I'm missing something obvious

jukbot commented 4 years ago

Just FYI for other who got this issue. It seems that the document is missing response promise chain, so it should be

const requestData = await $http.get('http://icanhazip.com')
const ip = await requestData.text()
return { ip }
lcherone commented 4 years ago

@jukbot yup.. but rtm: https://http.nuxtjs.org/guide/usage.html#using-in-asyncdata