0

In my Angular app, I am storing an array of Message objects within a Conversation object.

I was able to add a new Message to a hard-coded Conversation with the below code:

private _conversations = new BehaviorSubject<Conversation[]>([]);

get conversations() {
    return this._conversations.asObservable();
}

addMessageToConversation(conversationId: string, message: string) {
    return this.conversations.pipe(
      take(1),
      tap(conversations => {
        const updatedConversationIndex = conversations.findIndex(convo => convo.id === conversationId);
        const updatedConversations = [...conversations];
        const convoToUpdate = updatedConversations[updatedConversationIndex]; 
        convoToUpdate.messages.push(
          new Message(
            Math.random().toString(),
            message,
          )
        );
        this._conversations.next(updatedConversations);
      })
    );
  }

I made the below changes to this method so that I could update the object in a Firebase DB:

addMessageToConversation(conversationId: string, message: string) {
    let updatedConversations: Conversation[];
    return this.conversations.pipe(
      take(1), switchMap(conversations => {
        console.log(conversations);
        const updatedConversationIndex = conversations.findIndex(convo => convo.id === conversationId);
        updatedConversations = [...conversations];
        const convoToUpdate = updatedConversations[updatedConversationIndex];
        convoToUpdate.messages.push(
          new Message(
            Math.random().toString(),
            message,
          )
        );
        return this.http.put(
          `myUrl/conversations/${conversationId}.json`,
          { ...convoToUpdate, id: null }
        );
      }), tap(() => {
        this._conversations.next(updatedConversations);
      })
    );
  }

But now when I call this method, I get this console error:

conversations.findIndex is not a function

This error is in relation to this line of code:

const updatedConversationIndex = conversations.findIndex(convo => convo.id === conversationId);

Also, when I console.log(conversations) inside switchMap, this is logged:

{id: "-M52u0Fwdt5a9xE0wwCk", userId: "user3", mechanicId: "user2", messages: Array(1)}

Can someone please tell me why this is happening, & what changes are required to get it working?

1

1 Answer 1

1

If you are sure that conversations is an array, you can just set the type of it to any

...
switchMap((conversations: any) => {
     const updatedConversationIndex = conversations.findIndex(convo => convo.id === conversationId);
     updatedConversations = [...conversations];
...

Or even better create a type for it.

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

7 Comments

Thanks for your answer, but this code gives me the same error. I am showing what conversations is in my question aboce now
So that is an object, not an array. Maybe you want to use findIndex on the messages?
Yep, so what I want do here is add / push a new Message to the Message array inside Conversation
You can directly use conversations.messages because you have already the the only one via take(1). No more filtering is needed
OK, I will try to get my code working based on this. Is it possible for you to show how it should be done above in your answer?
|

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.