Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs rxjs错误的依赖请求:可观察{u isScalar:false,{u subscribe:f}_Reactjs_Typescript_Rxjs - Fatal编程技术网

Reactjs rxjs错误的依赖请求:可观察{u isScalar:false,{u subscribe:f}

Reactjs rxjs错误的依赖请求:可观察{u isScalar:false,{u subscribe:f},reactjs,typescript,rxjs,Reactjs,Typescript,Rxjs,我是Rxjs(^6.5.3)的新手。我正在使用它从我的react应用程序的api获取数据。 我提出两个请求,一个依赖于另一个。 我不知道我做错了什么,但我得到了这个错误: // console ouput Observable {_isScalar: false, _subscribe: f} Observable {_isScalar: false, _subscribe: f} ..... 显示结果的示例如下: // users endpoint { "data": {

我是Rxjs(^6.5.3)的新手。我正在使用它从我的react应用程序的api获取数据。 我提出两个请求,一个依赖于另一个。 我不知道我做错了什么,但我得到了这个错误:

// console ouput
 Observable {_isScalar: false, _subscribe: f}
 Observable {_isScalar: false, _subscribe: f}
.....
显示结果的示例如下:

// users endpoint
  {
   "data": {
    "total": 130,
    "users": [ // this format is also used as the User interface for typescript type check
        {"id": 1, "name": "John Doe", "profile_url": "https://myapi.co/user/id/1"},
        {"id": 2, "name": "Johny Doe", "profile_url": "https://myapi.co/user/id/2"}, ...
     ]
  }
 }


// user details endpoint
  {
   "data": {
    "info":{"name": "John Doe", "age": 50, "gender": "male", "status": "active", ...}
    }
 }
下面是我处理从api获取数据的代码

 // User class
class User{
.....
   private function getAllUsers(): Observable<User[]> {
     return from(fetch(api.getUsers()).then(res => res.json()).then(res => res.data.users))
   }

   private function getUserDetails(url: string): Observable<User> {
     return from(fetch(api.getUserDetails(url)).then(res => res.json()).then(res => res.data.info))
   }

   public function getUsers(): Observable<User[]> {

    return this.getAllUsers()
      .map(users => users.map(user => user.profile_url))
      .flatMap(profiles => {
        // console.log('flatmap: ', profiles.map(profiles => this.getUserDetails(profile)))
        return r.map(x => 
          this.getUserDetails(profile)
          );
      })
      .map(users => users);
   }
}

// index page
import ...
....

 const userClass = new User();

 userClass.getUsers()
 .subscribe(users => {
 console.log('users: ', users);
 })
//用户类
类用户{
.....
私有函数getAllUsers():可观察{
从(fetch(api.getUsers()).then(res=>res.json()).then(res=>res.data.users))返回
}
私有函数getUserDetails(url:string):可观察{
从(fetch(api.getUserDetails(url)).then(res=>res.json()).then(res=>res.data.info))返回
}
公共函数getUsers():可观察{
返回此文件。getAllUsers()
.map(users=>users.map(user=>user.profile\u url))
.flatMap(配置文件=>{
//console.log('flatmap:',profiles.map(profiles=>this.getUserDetails(profile)))
返回r.map(x=>
this.getUserDetails(配置文件)
);
})
.map(用户=>用户);
}
}
//索引页
导入。。。
....
const userClass=新用户();
userClass.getUsers()
.订阅(用户=>{
console.log('users:',users);
})
我发现了类似的问题


更新1:将返回的Observable类型替换为
Observable
Observable
我认为这里有一些问题。我建议替换
Observable的类型,并对映射进行一些很好的讨论

flatMap/mergeMap-立即为任何源项创建一个可观察项,所有以前的可观察项都保持活动状态

concatMap-在创建下一个可观察对象之前,等待上一个可观察对象完成

switchMap-对于任何源项,完成上一个可观察项并立即创建下一个可观察项

exhaustMap-当上一个可观察项未完成时,忽略源项


您要等待外部可观察对象-
getAllUsers
-完成,然后再启动内部可观察对象-
getUserDetails
-对吗?您可能正在寻找concatMap或switchMap而不是flatmap。

因此,我在代码和帖子中都更改了返回的observable的类型。接下来我得到的是这个类型检查错误:
type'Observable'不可分配给type'Observable'。类型“Observable”缺少类型“User[]”中的以下属性:长度、弹出、推送、连接和18个以上。ts(2322)
。当我注释掉内部可观察对象时,错误消失了。我找到了一个适合我的解决方案:。但是这个解决方案在打印结果之前要等待所有可观察到的结果完成(需要时间,500+个结果大约需要5分钟)。