Learn how to transition from storing user input in an array to using Firebase's push method to store data in a database. By leveraging Firebase's push method, we can create a single source of truth for our application data.
We'll explore the process of adding the push method to imports, deleting the existing array, and pushing user input to the specified database reference. With this approach, we eliminate the need to store conversation data in the array and address the challenges of editing instructions and clearing the conversation.
Instead, we keep the instruction object easily accessible in index.js and include it in the array sent to the OpenAI API during the FetchReply process. Discover the benefits of using Firebase to store user data and how it simplifies managing conversation arrays.
[00:00] Right here, we're pushing our user's input to ConversationArray, but now we want to store the user's input in the database. That is going to be the single source of truth. So let's use Firebase's push method to push it to the database instead. To get access to the push method, we need to add it to the list of imports.
[00:18] So I'll come up here and add it in right there. And then back down in the event listener, let's come in here and we're going to delete ConversationArray. And now we've got push, and that is no longer the standard JavaScript array method, that is now the Firebase push method.
[00:35] And what we need to pass it is the reference of the database we want to add to. And the reference to that database we've got up here, it is ConversationInDB. So we'll say push, and then in brackets, ConversationInDB, a comma, and then the object which we want to push.
[00:54] And that is, of course, exactly the same object. And it's as easy as that. So now let's go ahead and type something in here. And we'll hit send. And let's take a look at what we've got in the database. And there we are. It has appeared. If you log into your real-time database, you'll pretty much instantly see
[01:13] exactly what we've just pushed to the database. So we've got the content, hello, know it all, and the role of user. So that is our object. Now doing that leaves ConversationArray looking pretty redundant. We don't want to store our conversation here anymore. But that leaves us with an issue.
[01:31] We've got this instruction object, and we need to keep it somewhere. But storing it in the database with the rest of the conversation has actually got two disadvantages. Number one, if we ever want to edit the instruction to change the chatbot's personality or behavior, we'll actually have to edit the database directly.
[01:49] And number two, when we want to clear the conversation, we're going to get caught up in JavaScript spaghetti, making sure we don't delete the instruction, but do delete everything else. So we'll be making work for ourselves. The solution that I think is best is that we keep the instruction right here in index.js, where it's easily accessible and easily editable.
[02:09] And we just add it to the array we're sending to OpenAI with each request. So what I'm going to do is change ConversationArray to instruction object. And now it's an object. Let's delete the square brackets. And when we update FetchReply, we'll add this to the array we send off to the OpenAI API.
[02:30] And actually, updating FetchReply is what we need to come on to 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!