This is a learning struggle, and I might just be approaching this from an awkward angle, but guidance is appreciated. This project is using just HTML, Node, and Express.
I have a small app that is supposed to take survey inputs to build a 'friend' profile (essentially only a name, imgURL, and 7 question answers each a value 1-5). I want to save all the Friend objects with those properties in a JSON file in a data
sub-directory so that as new users are added, I can compare the friend objects against one another.
On my front end, I want to step through each stage one bootstrap card at a time, so I have a questions.js
file that manipulates the DOM to start with the name and imgURL and create a new 'Friend' object, then go one question at a time (printing question text from an array of strings) and capturing radio button selections (values 1-5) on("submit")
. End result is I have iterated through all the questions, grabbed the submitted values, and pushed them to a 'scores' array within the friend object, resulting thusly:
{
"name": "Ahmed",
"photo": "https://media.licdn.com/mpr/mpr/shrinknp_400_400/p/6/005/064/1bd/3435aa3.jpg",
"scores": [5, 1, 4, 4, 5, 1, 2, 5, 4, 1]
}
All of this works nicely, and I end up with good User navigation through the questions and a new Friend object with all my desired data persisting in the linked JS file.
Now how the ding-dang do I push that new Friend object as an array item in my friends.json
file? I kind of get that I need maybe an Ajax call within the questions.js
, or maybe I'm off-base and this belongs within an app.post
route in my server.js
, but so far my research and trials have only served up more confusion.
This might be a wack question, but I think there is a best practice in there I need to learn. Any insight is much appreciated!