Add Question/Answer Object to our Data with Node.js writeFile

Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated a year ago

All of our questions have been hard coded so far. We want the user to be able to add a question when they pass the --add flag.

We will need to use inquirer to ask for a question and it's answer. Once we have a question and answer in hand we will read our data.json file and then write to it with our new question. While we do this, we will assign a unique id to the question based on the length of the array being stored in our file.

Instructor: [0:00] We have this add function that's being triggered when I pass -a or --add to the end of my command line, so if I was to do that. At the moment, we don't have an addQuestion function, so let's work on that now. [0:15] If we check our readme, our addQuestion wants to start a dialogue, ask for the question, ask for the answer, and store that for a future user. Let's just say "Hello, let's add a new question!" Ask for a question, ask for an answer.

[0:30] Let's get the responses from an await of inquirer.prompt. Remember, inquirer.prompt takes an array of questions it's going to iterate over. There's two questions we want to ask. They both have type input.

[0:44] The first one will have a name of maybe targetQuestion, and the second one will have a name of targetAnswer. There's another message of, "What is your question?" This will have a message of, "What is your answer?"

[0:56] When that's done, let's just console.log responses to make sure we're on the right track. What's your question? What is the smallest unit of memory? The byte. Great. We get target question and target answer. We've done these two bits.

[1:16] The last thing we want to do is store for future uses. What we'll have to do here, we want to open the current file, so the current data going to =awaitfs.readFiledata.JSON. It wants to parse that so we can use it. Const parsed data is going to be a JSON.parse() version of our data.toString().

[1:43] Then we want to push to our parse data to our new question. Our new question needs an ID and it needs a question which is going to be in responses.targetquestion and I need an answer, responses.targetanswer. ID let's set those to be determined for now.

[2:03] Then finally, we want to await and we want to write, fs.writeFile(). We want to write to or CM file data.JSON and we want to string a pipe version of our party dinner. Let's test out on that. What's your question, which planet is the hottest in the solar system? The Venus.

[2:25] We're still logging or also writing. If we go check our data file that will end of our data file will see which planet is hottest in the solar system. Last thing we need to do is to work out what to do about this ID. You create a function that will generate the ID. That's going to be a functional take, the array of our data, and we'll get all of the IDs.

[2:47] Data.map item and just item.id. We now have an array of IDs. If we use the function math.max. Let's spread these numbers inside of there and this will give us the maximum ID. Now, we want one more than the current maximum ID that we're going to return. Let's update our function here, get ID and pass in ourparsed data, and then let's test it.

[3:10] How many hearts does an octopus have? Now, if we check our data object and see, how many hearts an octopus have, that has an ID of seven, which is the greatest possible value so far. Then if we ask how many hearts does an octopus have, three, and I got it right. We can see everything being updated and we've achieved all of this as listed are readme.

egghead
egghead
~ 2 hours ago

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