What does this error mean — Uncaught TypeError: Already read?

JavascriptPromiseTypeerror

Javascript Problem Overview


In Javascript, when is this error thrown?

enter image description here

index.js

/**
 * Created by tushar.mathur on 24/12/15.
 */
'use strict'

const _ = require('lodash')
const Rx = require('rx')
const createDataStore = require('./src/createDataStore')

const fetch = x => Rx.Observable.fromPromise(window.fetch(x)) 
const parseJSON = x => Rx.Observable.fromPromise(x.json()) // Line: 11 (Where the exception is thrown)
var create = _.partial(createDataStore, fetch, parseJSON)
module.exports = {
  create,
  // Alias for legacy purposes
  createDataStore: create,
  createFetchStore: create
}

Is it a native promise error? What does it imply? Google shows no result found.

Javascript Solutions


Solution 1 - Javascript

I think it means that the body has already been read by using either .json() .text() etc... When you run x.json() it takes the response's body and reads it into JSON. If you try to run x.json() again it will give you that error. So you could only use one of these methods once. So I am assuming somewhere in your code it is reading the body of the same response again using one of the Body methods.

I think that is why they offer the Body.bodyUsed method. So you can see if it has been read already.

Solution 2 - Javascript

This error means you have resolved the promise (in this case, you use Body.json()) more than once.

You can check the response body methods from the ref I attached below and you need a flag to check if the promise has been resolved or not: in this case, you can use Body.bodyUsed

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Response

HTH

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontusharmathView Question on Stackoverflow
Solution 1 - JavascriptgkkirschView Answer on Stackoverflow
Solution 2 - JavascriptWinters HuangView Answer on Stackoverflow