In this lesson of the course, we delve into updating the database and rendering the conversation in a chatbot application. We learn that the ConversationArray is the source of truth, holding the data retrieved from the database. The challenge is to add completion to the database and ensure the chatbot functions correctly.
By utilizing the push method and providing the necessary object, we successfully make updates to the database. However, we discover an issue where previously hidden conversations appear after a browser refresh. To address this, we need to render the existing conversation when the app loads and implement a "start over" button for users to reset the conversation. These tasks ensure a better user experience, allowing users to clearly see their previous messages and enabling the option to start fresh if needed.
[00:00] This line of code right here, which sends the completion to render typewriter text, is absolutely fine. We don't need to change that. But here, we don't want to update ConversationArray.
[00:13] Remember, ConversationArray is now here, and it just holds the array we get back from the database when we use this get method. Our single source of truth is on the database. So what we actually need to be doing right here is updating the database. So here is a challenge for you.
[00:31] I want you to add the completion to the database and then ask the chatbot something to check it's working. Now, to complete this challenge, you can look back at the code we've already written. And remember, this code here gives us an object which is already formatted in the way we need it.
[00:48] OK, pause now, get this challenge done, and I'll see you back here, and we'll have a look together. OK, hopefully you managed to do that just fine. So we need to use the push method. And we need to pass it the database ref, which is conversation in DB.
[01:10] And then we give it the object we want to push, and we can do that with this code right here. OK, let's delete this line of code. And we'll save and give it a try. I'll ask it a simple question. And it's given me a reply. It's looking good. Let's see if we've successfully updated the database.
[01:29] So this is what we originally had in the database. We've made a few changes to it since then. And now, look, we've got my question, what is a Frisbee? And the answer, a flying disc. So the code that we just wrote right here is definitely working. OK, so things are going well, but we've got a bit of an issue.
[01:46] If I just refresh the mini browser, in the database, we have got a whole conversation that I don't know about as a user. It's not shown here. And that's a problem because if I now start typing a question, I'm not aware that I'm actually continuing a conversation.
[02:02] And in fact, I might well have forgotten what I wrote before. So that means we've got two more jobs to do. We need to render the existing conversation out when the app loads so the user can see what they've already said.
[02:15] And we also need to wire up this start over button so that a user can reset the conversation whenever they want. So let's start on the first task of rendering out the conversation on load next.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!