user9847788
user9847788

Reputation: 2445

Property 'subscribe' does not exist on type 'void' in Angular app

Below is the onSendMessage() method in my Angular app:

onSendMessage() {
      this.conversationsService.addConversation(this.form.value.message).subscribe(() => {
        this.router.navigateByUrl('conversation-list');
      });
    });
  }

This method takes the user-entered message, & creates a record in firebase before re-routing the user.

This was working fine previously, but I made some changes to the ConversationsService.addConversation() method below.

Now when I try to compile my code I get this terminal error:

Property 'subscribe' does not exist on type 'void'

Here is the ConversationsService.addConversation():

addConversation(message: string) {
    let generatedId;
    let newConversation: Conversation;
    this.authService.userId.pipe(
      take(1),
      switchMap(userId => {
        newConversation = new Conversation(
          Math.random().toString(),
          userId,
          [new Message(
            Math.random().toString(),
          )]
        );
        return this.http.post<{ name: string }>(
          'firebaseUrl/conversations.json',
          { ...newConversation, id: null }
        );
      }),
      switchMap(resData => {
        generatedId = resData.name;
        return this.conversations;
      }),
      take(1),
      tap(conversations => {
        newConversation.id = generatedId;
        this._conversations.next(conversations.concat(newConversation));
      })
    );
  }

Can someone please tell me what changes need to be made to onSendMessage() so that this method works as expected again?

Also, here is the AuthService code where I am getting userId:

private _user = new BehaviorSubject<User>(null);

get userId() {
    return this._user.asObservable().pipe(
      map(user => {
        if (user) {
          return user.id
        } else {
          return null;
        }
      })
    );
  }

Upvotes: 0

Views: 1186

Answers (2)

DrNio
DrNio

Reputation: 1976

Exactly as @Quentin wrote.

Would be nice to add the return type of id to avoid similar issue in the future.

get userId(): Observable<number | null>, this assumes type of id is number. Also, you might not need that if/else statement. Just return user.id if it is not nullable.

Upvotes: 0

Quentin Grisel
Quentin Grisel

Reputation: 4987

Add the return keyword on the this.authService.userId.pipe(...)

Upvotes: 1

Related Questions