[英]Modifying object at service layer in angular2: Subscription is not assignable to the type observable
I am new to Angular2 framework so excuse if this is a basic question. 我是Angular2框架的新手,因此如果这是一个基本问题,请原谅。
I am handling an object at service layer returned via a http wrapper. 我正在通过HTTP包装返回的服务层处理对象。 This object should be passed to the component, but first the data should be manipulated and only necessary data extracted and forwarded.
该对象应该传递给组件,但是首先应该对数据进行操作,并且仅提取和转发必要的数据。
When I attempt to do this i receive an error "Subscription is not assignable to the type observable". 当我尝试执行此操作时,我收到错误消息“无法将订阅分配给可观察的类型”。 Here is the code
这是代码
return this.httpGet<UserInfo[]>(url, {headers}).switchMap(data => data)
.subscribe(
data => {
console.log("new values are going to be" + JSON.stringify( data[0].address.addressLine1));
this.userInfo[0].name = "test name";
this.UserInfo[0].address.city = data[0].address.city;
this.UserInfo[0].address.state = data[0].address.state;
},
);
Now, what makes sense in my mind is to first convert the data inside the subscription as it is streamed back, and then return the object explicitly but this also fails to give the desired output, even if the type is an observable. 现在,我认为有意义的是,首先在订阅中将其转换回流时将其转换为数据,然后显式返回该对象,但这也无法提供所需的输出,即使类型是可观察的也是如此。 Coming from a java oop background I cannot see what is the issue here an am thoroughly confused.
来自Java oop背景,在这里我看不到问题是什么,这是一个非常困惑的问题。 Any insight is appreciated.
任何见解均表示赞赏。
edit : for completion here is where the service call is made - 编辑:为了完成,在这里进行服务呼叫-
this.inputFormControl.valueChanges
.debounceTime(4000)
.switchMap(query => this.userService.search(query))
.subscribe(
data => {
this.data = data;
},
);
In your current code, you have already subscribed to the observable. 在您当前的代码中,您已经订阅了观察者。 So that returns a Subscription instead of an observable .
这样就返回了Subscription而不是Observable。 Then in your calling function, you are trying to assign this to a observable.
然后在您的调用函数中,尝试将其分配给可观察对象。 Try following pattern
尝试以下模式
someFunction() {
return this.httpGet<UserInfo[]>(url, {headers}).switchMap(data => data)
}
callingFuction(){
someFunction().subscribe(
data => {
console.log("new values are going to be" + JSON.stringify( data[0].address.addressLine1));
this.userInfo[0].name = "test name";
this.UserInfo[0].address.city = data[0].address.city;
this.UserInfo[0].address.state = data[0].address.state;
},
);
}
As mentioned in other answer, you are returning a subscription instead of an Observable. 如其他答案中所述,您将返回订阅而不是Observable。 Also I don't see why the use of
switchMap
would be necessary in your service (I assume you have your http request in a service), as you are using it in your component. 我也看不出为什么在服务中需要使用
switchMap
(我假设您在服务中有http请求),因为您正在组件中使用它。 So I would do map
in service, modify the data there and return it to your component. 因此,我将在服务中进行
map
在那里修改数据并将其返回给您的组件。
To point out one more thing, that in your current code you are not returning anything from the http-request, so you need to add a return statement. 再指出一件事,在当前代码中,您不会从http请求中返回任何内容,因此您需要添加一个return语句。
return this.httpGet<UserInfo[]>(url, {headers})
.map(data => {
this.UserInfo[0].name = "test name";
this.UserInfo[0].address.city = data[0].address.city;
this.UserInfo[0].address.state = data[0].address.state;
return this.UserInfo; // add return!
});
Your component code would stay as it is. 您的组件代码将保持原样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.