2016-01-14 27 views
23

在Javascript中,何時拋出此錯誤?這個錯誤是什麼意思 - Uncaught TypeError:已讀?

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 
} 

它是一個天然的諾言錯誤?它意味着什麼? Google顯示未找到結果。

回答

37

我認爲這意味着已經通過使用.json().text()等讀取正文。當您運行x.json()它需要響應的正文並將其讀入JSON。如果你嘗試再次運行x.json(),它會給你那個錯誤。所以你只能使用these方法之一。所以我假設你的代碼中的某個地方正在使用Body方法之一再次讀取相同響應的主體。

我認爲這就是他們提供Body.bodyUsed方法的原因。所以你可以看看它是否已被閱讀。

5

此錯誤表示您已經多次解決承諾(在這種情況下,您使用Body.json())。

您可以檢查從我下方安裝裁判響應體的方法,你需要一個標誌,檢查是否承諾已經解決或不:在這種情況下,你可以使用Body.bodyUsed

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

HTH

+1

我剛剛遇到這個錯誤。您不需要兩次解決承諾,無論如何都不可能解決承諾的內部屬性。你只需要調用'.then()'兩次。據記錄,支持調用'.then()'兩次。我會看看我是否可以簡化一個例子。我的代碼顯示在這裏:https://tonicdev.com/hippietrail/57611741f056621300ecd1e2 – hippietrail