Below are the Conversation & Message models I am using in my Ionic 5 / Angular app:
export class Conversation {
constructor(
public id: string,
public userId: string,
public mechanicId: string,
public messages: Message[]
) { }
}
And here is the Message model:
export class Message {
constructor(
public id: string,
public text: string,
public userId: string,
public timestamp: string
) { }
}
When a user creates a Conversation object, I want them to add 1 Message object within the Conversation.
Then when other uses are updating the Conversation (i.e. sending more messages), they will just be pushing another Message to the Conversation.
Here is what I have so far for creating a Conversation:
onSendMessage() {
this.conversationService.addConversation(
this.mechanicToContact.id,
this.form.value.message
);
}
I've tried the following method in my ConversationService:
addConversation(mechanicId: string, message: string) {
const newConversation = new Conversation(
Math.random().toString(),
this.authService.userId,
mechanicId,
new Message(Math.random().toString(), message, this.authService.userId, mechanicId)
);
}
But I'm getting this error when trying to create the new Message:
Argument of type 'Message' is not assignable to type parameter of 'Message[]'
I'm not sure how I should pass the remaining attributes of the Message. Can someone please tell me how this is done?