使用node以json格式将html格式的数据保存在.json文件中,并使用javascript表示 [英] Save html-form data in json format in a .json file using node and express with javascript
问题描述
节点和快递中的新手 我正在从html表单获取用户输入,并尝试在.json文件中追加或推送. 我使用了 jsonfile npm-package,但是它不是以json的数组格式出现的
Newbie in node and express I am taking user input from html-form and trying to append or push it in a .json file. I have used jsonfile npm-package but it is not coming in a array format of json
附加代码-
var express = require('express');
var app = express();
//jade --> ejs -->html
app.engine('html', require('ejs').renderFile);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
var jsonfile = require('jsonfile');
var file = './userdata.json'
//trying to write via form to json
app.post('/gettingdata', function(req, res) {
var user_id = req.body.usrid;
var token = req.body.usrphone;
var geo = req.body.usrdata;
//start writing
var obj = { name: user_id , phone: token, adress: geo }
jsonfile.writeFileSync(file, obj, {flag: 'a'});
//default
//res.send(user_id + ' ' + token + ' ' + geo);
});
html-
<body>
<form action="/gettingdata" method="post">
Name:<input type="text" name="usrid" /><br>
Phone:<input type="text" name="usrphone" /><br>
RData:<input type=="text" name="usrdata" /><br>
<input type="submit" value="Submit" >
</form>
</body>
json显示为-
{"name":"name1","phone":"8989898989","adress":"random1"}
{"name":"name1","phone":"767656568","adress":"randomdata1"}
{"name":"name1","phone":"767656568","adress":"randomdata1"}
在对象之间也没有逗号,也没有方括号.我需要它们能够解析,以便以后可以动态编辑和删除前端中的数据. 建议任何链接,方法或npm软件包.如果问题重复,也分享该链接
there are no commas appearing between objects and no square brackets. I need them to be able to make parsing possible, so that I can dynamically edit and delete data from my front-end later. Suggest any link,method or npm package to do so.If question repeated share the link of that too
推荐答案
我将用一些代码扩展Shil的答案:
I will expand on Shil's answer with some code:
// 1. Read the existing file
fs.readFile(file, (err, data) => {
if (err && err.code === "ENOENT") {
// But the file might not yet exist. If so, just write the object and bail
return fs.writeFile(file, JSON.stringify([obj]), error => console.error);
}
else if (err) {
// Some other error
console.error(err);
}
// 2. Otherwise, get its JSON content
else {
try {
const fileData = JSON.parse(data);
// 3. Append the object you want
fileData.push(obj);
//4. Write the file back out
return fs.writeFile(file, JSON.stringify(fileData), error => console.error)
} catch(exception) {
console.error(exception);
}
}
});
这只是一个快速的说明性示例:随着文件的增长,效率很低,因为每次必须读写整个文件.
This is just a quick, illustrative example: it is inefficient as the file grows as it has to read and write the entire file every single time.
请注意,这将创建一个包含对象数组的文件,这是在JSON中创建对象列表的方式.因此,您的最终输出将如下所示:
Note that this will create a file which contains an array of objects, which is how you make lists of objects in JSON. So your final output will look like this:
[
{"name":"name1","phone":"8989898989","adress":"random1"},
{"name":"name1","phone":"767656568","adress":"randomdata1"},
{"name":"name1","phone":"767656568","adress":"randomdata1"}
]
这篇关于使用node以json格式将html格式的数据保存在.json文件中,并使用javascript表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!