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}))
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648049a4638090.html
评论列表(0条)