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.

Share Improve this question asked Feb 5, 2018 at 8:44 MerimMerim 1,3472 gold badges24 silver badges40 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 5

When 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.

Share Improve this question asked Feb 5, 2018 at 8:44 MerimMerim 1,3472 gold badges24 silver badges40 bronze badges 1
  • 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
Add a ment  | 

1 Answer 1

Reset to default 5

When 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