1

I have obeservables of these objects :

  {
        id : "f3055770-6e66-4936-8e9a-732b53121549"
        message:"Empty Notification for test ......"
        navigationLink:"/fusion/home"
        seen:false 
        sendedOn:"2016-12-02T15:19:44.856Z"
        userId :null
      }

I would like to not receive duplicated object (based on ids) and i use this method to achieve it

 notify(userID: string) {
    return Observable.interval(5000)
        .map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID)
        .switchMap(url => {
            return Observable.from(this.datatService.get(url));
        })
        .flatMap(response => Observable.from(response.json().slice()))

}

When i add distinct(x => x.id) as last operator i have only one object instead of Four any help ?

UPDATE :

I call this method in the oninit() life cycle of my component, so that the method execute every 5 second to get notification, i use distict this way :

 notify(userID: string) {
    return Observable.interval(5000)
        .map(() => this.baseUrl + '/notification/GetUnseen?userId=' + userID)
        .switchMap(url => {
            return Observable.from(this.datatService.get(url));
        })
        .flatMap(response => Observable.from(response.json().slice()))
        .distinct(x => x.id);
}

UPDATE 2

Serveur raw response :

"[{"userId":null,"sendedOn":"2016-12-02T15:19:44.856Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"f3055770-6e66-4936-8e9a-732b53121549"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.146Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"ce172122-11d9-4054-a3e4-594c8c910a7d"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.146Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"66e32c45-f544-4ce6-901c-e5ac64904954"},{"userId":null,"sendedOn":"2016-12-02T15:19:45.147Z","message":"Empty Notification for test ......","navigationLink":"/fusion/home","seen":false,"id":"4c2322cb-526c-490e-8a86-f1e9ced1c34f"}]"
5
  • Can you please give the context: 1) How do you call this method? 2) Where do you add the distinct operator? Commented Dec 5, 2016 at 9:26
  • Are you sure those four objects don't have the same id? Commented Dec 5, 2016 at 9:38
  • Updated, @martin yeah am shure that the ids are not the same Commented Dec 5, 2016 at 9:48
  • @NacimIdjakirene Can you show the raw response from server before you pass it to .json()? Commented Dec 5, 2016 at 9:59
  • See the updated question Commented Dec 5, 2016 at 10:05

1 Answer 1

1

I found the solution for the distinct !

In fact, the function passed to disctinct can take 2 parameters (precedentValue, actualValue) so you can resolve this issue like this :

....
....
.distinct(function (x, y) { return x.id === y.id; });

It return a boolean on each iteration (is x.id = y.id ? => true or false...).

**UPDATE **

My mistake is that i was looking for the rxjs 4.0 documentation, and my project is on rxjs 5 .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.