I've been working on integrating a chatbot based on RiveScript into Angular. The chatbot is functioning well - I always see the correct response in the console. Displaying the user's input is also working seamlessly. However, I'm encountering an issue when trying to display the chatbot's response in the chat UI due to the following error:
ERROR Error: Uncaught (in promise): TypeError: this is undefined
I've experimented with various solutions but haven't been able to pinpoint the problem. It's slightly perplexing because I can retrieve the correct chatbot message in the console. Any assistance in resolving this issue would be greatly appreciated!
Below is my source code along with comments and a console log for reference. This should demonstrate that the code is functional.
converse(msg: string) {
const userMessage = new Message(msg, 'user'); // capturing user message
this.update(userMessage);
var bot = new RiveScript({utf8: true}); // initializing bot
bot.loadFile('/assets/brain/test.rive').then(loading_done); // loading bot brain
function loading_done() {
console.log("Chatbot initialized!");
bot.sortReplies(); // sorting replies
let username = "user";
return bot.reply(username, msg).then(answer => { // fetching chatbot answer
console.log("User: " + msg);
console.log("Chatbot: " + answer);
const result = answer;
const botMessage = new Message(result, 'bot');
this.update(botMessage);
});
}
}