fetchapi和rxjs:我看不到回复中的任何数据

发布于 2025-02-12 21:38:30 字数 1112 浏览 1 评论 0原文

我有以下代码,

import { fromEvent } from 'rxjs';
import { debounceTime, map, mergeMap, tap } from 'rxjs/operators';

// Examples: kumar
const url = 'https://gorest.co.in/public/v2/users?name=';

// elem ref
const searchBox = document.getElementById('search');

// streams
const keyup$ = fromEvent(searchBox, 'keyup');

keyup$
  .pipe(
    map((i: any) => i.currentTarget.value),
    debounceTime(500),
    mergeMap((v) => fetch(url + v)),
    tap((result) => {
      console.log('JSON: ', result.json());
    })
  )
  .subscribe((result) => console.log(result));

您可以将其复制并粘贴到 https://stackblitz.com/edit/typescript-adheqt?devtoolsheight=50&file=index.ts

但我看不到JSON结果。请帮助,我不明白为什么我看不到任何数据。

PS:我不想使用fromfetch。它只是用于学习目的,我想使用Fetch API。

I have the following code

import { fromEvent } from 'rxjs';
import { debounceTime, map, mergeMap, tap } from 'rxjs/operators';

// Examples: kumar
const url = 'https://gorest.co.in/public/v2/users?name=';

// elem ref
const searchBox = document.getElementById('search');

// streams
const keyup$ = fromEvent(searchBox, 'keyup');

keyup$
  .pipe(
    map((i: any) => i.currentTarget.value),
    debounceTime(500),
    mergeMap((v) => fetch(url + v)),
    tap((result) => {
      console.log('JSON: ', result.json());
    })
  )
  .subscribe((result) => console.log(result));

You can copy and paste it into https://stackblitz.com/edit/typescript-adheqt?devtoolsheight=50&file=index.ts

But I don't see the JSON result. Please help, I don't understand why I don't see any data.

PS: I don't want to use fromFetch. It's just for learning purposes and I would like to use the fetch api.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-02-19 21:38:30

JSON()返回承诺,因此您仍然需要解决该承诺/可观察的诺言:

keyup$
  .pipe(
    map((i: any) => i.currentTarget.value),
    mergeMap((e) => fetch(`https://gorest.co.in/public/v2/users?name=${e}`)),
    mergeMap(response => response.json()), // json() resolved using mergeMap
    debounceTime(500)
  )
  .subscribe(console.log);

json() returns a promise so you still need to resolve that promise/observable :

keyup$
  .pipe(
    map((i: any) => i.currentTarget.value),
    mergeMap((e) => fetch(`https://gorest.co.in/public/v2/users?name=${e}`)),
    mergeMap(response => response.json()), // json() resolved using mergeMap
    debounceTime(500)
  )
  .subscribe(console.log);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文