简体   繁体   English

rxjs observable.of(object) 返回 {“_isScalar”: “”, “value”: {}, “scheduler”: “” } 而不是 value

[英]rxjs observable.of(object) returns {“_isScalar”: “”, “value”: {}, “scheduler”: “” } instead of value

I am fairly new to rxjs.我对 rxjs 相当陌生。

I tried to create a function that runs a angular 2 http request like that:我尝试创建一个运行 angular 2 http 请求的函数,如下所示:

syncFromApi$() {

  // create Headers
  ...

  let ob$ = this.http.post(
    CONFIG_APP.apiEndpoint + '/sync/app',
    '',
    { headers: headers }
  )
  .map((response:Response) => {
    return Observable.of({type: "success", payload: response.json()});
  })
  .catch(function (err:Response) {
    return Observable.of({type: "error", payload: err.json()});
  });

  return ob$;
}

In short, when having success, I am returning an Object which indicates, if the request was successful.简而言之,当成功时,我返回一个对象,指示请求是否成功。 After that I want to switch map depending on the returned object:之后我想根据返回的对象切换地图:

let o$ = syncFromApi$()
  .switchMap((value) => {
    console.log("value", JSON.stringify(value, null, 0));
    if (data.type === 'success') {
      return Observable.of(1);
    } else if (data.type === 'error') {
      return Observable.of(2);
    }
  });

And here comes the problem I face.这就是我面临的问题。 The console.log(...) outputs a object like this: console.log(...) 输出这样的对象:

value, {"_isScalar": true, "value": THIS_IS_WHAT_I_NEED_AS_VALUE, "scheduler": null}

But I need only the object I returned with the syncFromApi$ function.但我只需要使用 syncFromApi$ 函数返回的对象。

Can anyone explain to me, if I am using observable.of() in the wrong manner?谁能向我解释一下,如果我以错误的方式使用observable.of()

I tried observable.of(1) and it returns 1 as value.我试过observable.of(1)并且它返回 1 作为值。 If I use a object ( observable.of({ type: "myType", message: "myMessage" }) ), it returns an object wrapping the object I need with the keys "_isScalar", "value" and "scheduler".如果我使用一个对象( observable.of({ type: "myType", message: "myMessage" }) ),它会返回一个对象,用键“_isScalar”、“value”和“scheduler”包装我需要的对象。

Thanks in advance.提前致谢。

Your problem is in your map operator:您的问题出在您的map运算符中:

let ob$ = this.http.post(
  CONFIG_APP.apiEndpoint + '/sync/app',
  '',
  { headers: headers }
)
.map((response:Response) => {
  return {type: "success", payload: response.json()}; // <----
})

You need to return an object not an observable.您需要返回一个对象而不是可观察对象。 The map operator uses the object like it is and doesn't project the returned observable into the observable chain. map操作符按原样使用对象,并且不会将返回的 observable 投影到 observable 链中。

If you want to return Observable.of for some reasons, you need to use the flatMap operator instead:如果由于某些原因要返回Observable.of ,则需要改用flatMap运算符:

let ob$ = this.http.post(
  CONFIG_APP.apiEndpoint + '/sync/app',
  '',
  { headers: headers }
)
.flatMap((response:Response) => {
  return Observable.of({type: "success", payload: response.json()});
})

That's fine for the catch operator since it expects an observable to be returned into its associated callback.这对catch操作符来说很好,因为它期望一个可观察对象返回到其关联的回调中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM