Here are the definitions for the Conversation
and Message
models I am implementing in my Ionic 5 / Angular application:
export class Conversation {
constructor(
public id: string,
public userId: string,
public mechanicId: string,
public messages: Message[]
) { }
}
Below is the structure of the Message model:
export class Message {
constructor(
public id: string,
public text: string,
public userId: string,
public timestamp: string
) { }
}
When a user creates a new Conversation
instance, they should include one Message
object within the Conversation
.
Subsequently, if other users are updating the Conversation
(e.g. sending more messages), they will simply add another Message
to the existing Conversation
.
This is how I currently handle the creation of a Conversation
:
onSendMessage() {
this.conversationService.addConversation(
this.mechanicToContact.id,
this.form.value.message
);
}
I've attempted the following approach within 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)]
);
}
However, I encounter an error when trying to create a new Message:
An argument of type 'Message' cannot be assigned to a type parameter of 'Message[]'
I am uncertain about how to pass the remaining attributes of the Message
correctly. Can someone guide me through this process?