1

I'm think fundamentally i'm doing something wrong. I'm trying to find a single entity by pulling it from filtered list, if there isn't an entity there I need to create one.

I feel like this function is wrong because I should be returning an observable but instead returning nothing

getOrCreateNew(receiverId: number) : Observable<Conversation> {
    var userId = this.identity.userInfo.id;

    //TODO: you should be using an expression here, and having a builder for generating your
    //filters in case you want to switch them in the future
    var employerFilter = new PropertyFilterNode("EmployerUserId", FilterCondition.Equal, receiverId.toString());
    var employeeFilter = new PropertyFilterNode("EmployeeUserId", FilterCondition.Equal, userId.toString());
    let conversationFilter = new BinaryFilterNode(employerFilter, employeeFilter, Combiner.Or);

    this.getList(conversationFilter).subscribe(entities => {
        if (entities == null || entities.length == 0) {
            let conversation: Conversation;
            conversation.employerUserId = receiverId;
            conversation.employeeUserId = userId;
            return this.create(conversation);
        }
        else {
            let entity = entities[0];
            return Observable.of(entity); //.Return(entity)
        }
    });

    return null;
}

How can I return an observable which is return from inside subscribe?

1
  • how is getOrCreateNew used? It seems to return null but is typed as returning an observable. Also, you have type annotations in several places that may mask errors. Commented Nov 15, 2017 at 4:12

2 Answers 2

1

The way getList subscription works suggests that it is supposed to be mergeMap or switchMap (considering that create returns an observable as well):

return this.getList(conversationFilter).mergeMap(entities => {
    if (entities == null || entities.length == 0) {
        let conversation: Conversation;
        conversation.employerUserId = receiverId;
        conversation.employeeUserId = userId;
        return this.create(conversation);
    }
    else {
        let entity = entities[0];
        return Observable.of(entity); //.Return(entity)
    }
});

In this case an observable that is returned from getOrCreateNew should be subscribed in order to emit values because it isn't subscribed internally.

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

Comments

1

Place the return before this.getList also :

return this.getList(conversationFilter).subscribe(entities => {

2 Comments

Get list returns a Observable<Array<Conversation>> i need to use that value to then conditionally return the observable inside I need it to return Observable<Conversation>
then you should use mergeMap or flatMap instead of subscribe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.