I am currently in the process of creating a chatbot for an upcoming exhibition and encountered the following error:
src/app/chat/chat.component.ts:32:9 - error TS2322: Type 'undefined' is not assignable to type 'string'. 32 this.message = undefined; ~~~~~~~~~~~~
Below is the relevant section of my code:
import { Component, OnInit } from '@angular/core';
import { IChat } from '../interfaces/ichat';
import { ChatService } from '../services/chat.service';
@Component({
selector: 'app-chat',
templateUrl: './chat.component.html',
styleUrls: ['./chat.component.css']
})
export class ChatComponent implements OnInit {
chats: IChat[] = [];
message: string;
sending: boolean;
constructor(private _chatService: ChatService) { }
ngOnInit() {
// subscribe to pusher's event
this._chatService.getChannel().bind('chat', data => {
if (data.email === this._chatService.user.email) {
data.isMe = true;
}
this.chats.push(data);
});
}
sendMessage(message: string) {
this.sending = true;
this._chatService.sendMessage(message)
.subscribe(resp => {
this.message = undefined;
this.sending = false;
}, err => {
this.sending = false;
} );
}
}
Thank you