本文介绍了在Express Application中从JS文件追加JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一次学习上的挣扎,我可能只是从一个尴尬的角度来解决这个问题,但是我们对此给予了指导.该项目仅使用HTML,Node和Express.

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.

我有一个小应用程序,该应用程序应进行调查输入以构建朋友"个人资料(基本上只有一个名称,imgURL和7个问题,每个问题的答案都为1-5).我想将所有具有这些属性的Friend对象保存在data子目录中的JSON文件中,以便在添加新用户时,我可以将朋友对象彼此进行比较.

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.

在前端,我希望一次通过一个引导卡来逐步完成每个阶段,因此我有一个questions.js文件,该文件操纵DOM以名称和imgURL开头,并创建一个新的"Friend"对象,然后一次执行一个问题(从字符串数组中打印问题文本)并捕获单选按钮选择(值1-5)on("submit").最终结果是我遍历了所有问题,获取了提交的值,并将它们推到friend对象内的分数"数组中,从而得出:

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]
  }

所有这些都能很好地工作,最终我可以很好地引导用户浏览问题和新的Friend对象,并将所有需要的数据持久保存在链接的JS文件中.

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.

现在我该如何将新的Friend对象作为数组项推送到我的friends.json文件中?我有点理解,我可能需要在questions.js内进行Ajax调用,或者我不在基地内,并且这属于我server.jsapp.post路由之内,但是到目前为止,我的研究和试验仅适用于更加混乱.

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!

推荐答案

这篇关于在Express Application中从JS文件追加JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 15:39