⚠️ This lesson is retired and might contain outdated information.

Update fetchReply

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 months ago

In this lesson, we explore how to make changes to the FetchReply functionality. We want to send the entire conversation stored in the database to the OpenAI API, but first, we need to fetch the conversation using the get method from Firebase.

We chain a then function with a callback that receives a snapshot parameter containing the current data from the database. To ensure the snapshot exists, we use an if statement. If the snapshot is available, we log it out and then extract the JavaScript value using the val method from Firebase.

However, the extracted data still contains the Firebase identifier, which we do not want to send to the API. To convert the data into the correct format, an array of objects, we use the object.values method. Finally, we discuss the next steps in FetchReply and explain that we will focus on the instruction in the next section.

[00:00] The next thing we need to do is make some changes to FetchReply. We know we need to send the entire conversation with every request to the OpenAI API. But now that the conversation is stored on the database, we need to fetch it before we can include it with our request. And with Firebase, we do that with the get method.

[00:19] But before we can use the get method, we need to add it to our list of imports. So let's come up here and we'll just put it after push. Now, get is a method, so we'll give it the brackets. And we're going to pass in the reference to our database, just like we did with the push method.

[00:37] Now we're going to chain a then. And here we'll have a callback function. And that function gets a parameter snapshot. And snapshot is the data in our database as it exists at this time.

[00:50] Now, inside the body of this function, let's be safe and use an if just to make sure a snapshot exists. So if, for example, writing to the database right here in the event listener failed, then we would want to know about that. So let's just say else no data available.

[01:08] Because if what we send to the OpenAI API is not the array of objects it expects, it's going to give us an error anyway. So it's a good idea to check what's going on here. Now, in here, we can make our request to the OpenAI API. So let's just cut and paste all of this code.

[01:26] And let's just format that nicely. Now, we're actually not quite ready to call the API. So I'm actually going to comment out all of that code. And let's just come in here and log out snapshot. Now, remember, we've already got something in our database. So there should be something for us to log out.

[01:43] So rather than write anything more in here and add to the database, I'm just going to call the fetch reply function. Now, when I hit save, let's just see what we log out. Well, there we are. We can see the contents of our database. We've got an object.

[01:58] It's got a content key with hello, know it all and a role key with user. But at the beginning, it's also got a Firebase identifier of some kind. And we don't want to be sending that to OpenAI. It would just really confuse it. All we want is an array of objects. So let's check what the Firebase docs say.

[02:16] And they talk about this val method. And it says here it extracts a JavaScript value from a data snapshot. Well, that is exactly what we want to do. Now, if you'd like to read up more about Val in the Firebase docs, this screenshot is a link. So just click on it and it will take you straight there.

[02:34] So I'm going to come in here. And where we've got snapshot, I'm going to call Val. And let's just save and see what happens. OK, so on its own, it doesn't make any difference. We've still got Firebase's identifier and then our object. So let's just check something else we can use from MDN.

[02:52] And again, this screenshot is a link to this page on MDN. Now, object.values, let's see what it says here. It says the object.values static method returns an array of a given object's own enumerable string keyed property values. OK, that sounds like a bit of a mouthful.

[03:09] But the important thing here is it returns an array. And an array is what we want. So let's just experiment with it and see what we get. So I'll come in here and say object.values. And then in brackets, we'll have our snapshot on which we've called the Val method. OK, let's hit save.

[03:28] And look at that. Down in the console, we have got an array and it's got our object in it. And that is the exact correct format that we need to send to the OpenAI API. But we're not quite ready to send it yet. We need to think about the instruction.

[03:44] So let's do a little bit more work in Fetch Reply in the next scrim.

egghead
egghead
~ just now

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

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

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!

Markdown supported.
Become a member to join the discussionEnroll Today