admin管理员组文章数量:1026373
I need to send a put request to the API with ngrx/effect
but the request requires id and content so MongoDB could be able to find a post.
Here is the code, I tried to set two action payloads:
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public payload: string & Post ) { }
}
I need to send a put request to the API with ngrx/effect
but the request requires id and content so MongoDB could be able to find a post.
Here is the code, I tried to set two action payloads:
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public payload: string & Post ) { }
}
Here is the effect:
@Effect()
editPost$: Observable<Action> = this.actions$
.ofType(PostActions.UPDATE_POST)
.map((action: PostActions.UpdatePost) => action.payload)
.switchMap((?) => {
return this.postService.editPost(id, post);
})
.map(editedpost => ({type: PostActions.UPDATE_POST_SUCCESS, payload: editedpost}))
}
editPost()
requires two parameters: id and content.
I guess my action payload should be set the other way, I'm new to ngrx
so every advice is wele.
- you could define a payload made of 2 properties, or a constructor method that takes 2 arguments – Jota.Toledo Commented Feb 5, 2018 at 9:17
1 Answer
Reset to default 5When you declare a variable with type string & Post, it means that this variable owns all method of Post and all method of string. That is probably not what you want. You want that your action has a identifier (of type string) and a post. You can declare a interface for that, or your action can have several properties, as :
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public id: string, public payload: Post ) { }
}
So then in your effect your can write :
@Effect()
editPost$: Observable<Action> = this.actions$
.ofType(PostActions.UPDATE_POST)
.switchMap(action => {
return this.postService.editPost(action.id, action.post);
})
.map(editedpost => ({type: PostActions.UPDATE_POST_SUCCESS, payload: editedpost}))
}
I need to send a put request to the API with ngrx/effect
but the request requires id and content so MongoDB could be able to find a post.
Here is the code, I tried to set two action payloads:
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public payload: string & Post ) { }
}
I need to send a put request to the API with ngrx/effect
but the request requires id and content so MongoDB could be able to find a post.
Here is the code, I tried to set two action payloads:
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public payload: string & Post ) { }
}
Here is the effect:
@Effect()
editPost$: Observable<Action> = this.actions$
.ofType(PostActions.UPDATE_POST)
.map((action: PostActions.UpdatePost) => action.payload)
.switchMap((?) => {
return this.postService.editPost(id, post);
})
.map(editedpost => ({type: PostActions.UPDATE_POST_SUCCESS, payload: editedpost}))
}
editPost()
requires two parameters: id and content.
I guess my action payload should be set the other way, I'm new to ngrx
so every advice is wele.
- you could define a payload made of 2 properties, or a constructor method that takes 2 arguments – Jota.Toledo Commented Feb 5, 2018 at 9:17
1 Answer
Reset to default 5When you declare a variable with type string & Post, it means that this variable owns all method of Post and all method of string. That is probably not what you want. You want that your action has a identifier (of type string) and a post. You can declare a interface for that, or your action can have several properties, as :
export class UpdatePost implements Action {
readonly type = UPDATE_POST
constructor(public id: string, public payload: Post ) { }
}
So then in your effect your can write :
@Effect()
editPost$: Observable<Action> = this.actions$
.ofType(PostActions.UPDATE_POST)
.switchMap(action => {
return this.postService.editPost(action.id, action.post);
})
.map(editedpost => ({type: PostActions.UPDATE_POST_SUCCESS, payload: editedpost}))
}
本文标签: javascriptMultiple payload on NGRX actioneffectStack Overflow
版权声明:本文标题:javascript - Multiple payload on NGRX actioneffect - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745648047a2161144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论