我正在尝试使用来自lichess.org上的这个端点的数据。
下面是一个使用该数据流的React组件的最小工作示例。我正在使用一个名为坎-恩德森流的库。
import ndjsonStream from "can-ndjson-stream"
import { useEffect } from "react"
function App() {
useEffect(() => {
fetch("https://lichess.org/api/tv/feed")
.then(res => ndjsonStream(res.body))
.then(stream => {
const streamReader = stream.getReader()
streamReader.read().then(async res => {
while (!res || !res.done) {
res = await streamReader.read()
console.log(res.value)
}
})
})
.catch(console.error)
}, [])
return <>Lorem Ipsum</>
}
export default App但是,如果我试图编写相同的代码并在Node中运行它,如下所示:
import ndjsonStream from "can-ndjson-stream"
import fetch from "node-fetch"
fetch("https://lichess.org/api/tv/feed")
.then(res => ndjsonStream(res.body))
.then(stream => {
const streamReader = stream.getReader()
streamReader.read().then(async res => {
while (!res || !res.done) {
res = await streamReader.read()
console.log(res.value)
}
})
})
.catch(console.error)我知道这个错误:
ReferenceError: ReadableStream未在ndjsonStream中定义
因此,从fetch中提取的res似乎是空的或未定义的,但是获取其他API很好。
import ndjsonStream from "can-ndjson-stream"
import axios from "axios"
axios
.get("https://lichess.org/api/tv/feed")
.then(res => ndjsonStream(res.data))
.then(stream => {
const streamReader = stream.getReader()
streamReader.read().then(async res => {
while (!res || !res.done) {
res = await streamReader.read()
console.log(res.value)
}
})
})
.catch(console.error)但它只是挂起,没有显示输出。感谢任何人,谁可以澄清这一点,或提供任何替代方式来运行这一节点。
https://stackoverflow.com/questions/69506089
复制相似问题