0
votes

I have an observable that I want to modify.

private userProposalConversatorAfterLogin = new BehaviorSubject<any>([]);
    getUserProposalConversatorAfterLogin$ = this.userProposalConversatorAfterLogin.asObservable();
    currentUserProposalConversatorAfterLoginValue():ParcelModel[] {
      return this.userProposalConversatorAfterLogin.value;
    }
    setUserProposalConversatorAfterLogin(x) {
      this.userProposalConversatorAfterLogin.next(x);
    }

and here is the result with the subscription(this.getUserProposalConversatorAfterLogin$.suscribe(proposal))

{
id:1,
messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'}, .....]
}

I want to add a new message to the messages array for exemple: {id=3, comments:'oooo'} and and when i go to subscribe again i want to get:

  {
    id:1,
    messages:[{id:'1', comments:'hello'}, {id=2, comments:'aaa'},  {id=3, comments:'oooo'} .....]
    }

i tried many way but no success

getUserProposalConversatorAfterLogin$.pipe(
    switchMap(x => {
      const new_array = (x.filter(x => x['id'] === proposal_identifiant))
      console.log(new_array);
      if (new_array && new_array.length >=1) {
        new_array[0]['message_proposal_identifiant'].push(new_message);
        console.log(new_array[0]['message_proposal_identifiant']);
        
      }
      return new_array

      }),
    share(),
    distinctUntilChanged(),
  )

when i subscribe again i don't see the new value. I think we need to modify userProposalConversatorAfterLogin but how?

I tried with this.userProposalConversatorAfterLogin.next(val) but it adds just a new object Thank you in advance

1
Hi, is the solution given helped you? ;)Sivakumar Tadisetti
many thx my frienduser9714967

1 Answers

0
votes

As you are using BehaviorSubject you can call getValue() on it, which gives you the current value. And store that current value in a variable

Ex:

const currentValue = this.userProposalConversatorAfterLogin.getValue(); 

// In your case current value is an array, so you can do below

currentValue.push( {id=3, comments:'oooo'} ); 

// and then call .next() with updated current value

this.userProposalConversatorAfterLogin.next( currentValue );

So you will get all the three objects

Working Stackblitz