Javascript 这个错误是什么意思 - Uncaught TypeError: Already read?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34786358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:51:10  来源:igfitidea点击:

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

javascriptpromisetypeerror

提问by tusharmath

In Javascript, when is this error thrown?

在 Javascript 中,什么时候抛出这个错误?

enter image description here

在此处输入图片说明

index.js

索引.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.

这是一个原生的承诺错误吗?这意味着什么?谷歌显示未找到任何结果。

回答by gkkirsch

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 thesemethods 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.

我认为这意味着已经使用 .json() .text() 等读取了正文...当您运行 x.json() 时,它会获取响应的正文并将其读入 JSON。如果你再次尝试运行 x.json() ,它会给你那个错误。所以,你可以只使用一个这些方法一次。所以我假设在您的代码中的某个地方,它正在使用其中一种 Body 方法再次读取同一响应的正文。

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

我认为这就是他们提供这种Body.bodyUsed方法的原因。所以你可以看到它是否已经被阅读。

回答by Winters

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

此错误意味着您已经Body.json()不止一次解决了承诺(在本例中,您使用了)。

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

您可以从我在下面附加的参考中检查响应正文方法,并且您需要一个标志来检查承诺是否已解决:在这种情况下,您可以使用 Body.bodyUsed

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

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

HTH

HTH