Node.js 保存JSON数据时出错-NodeJS Express
我在保存和访问JSON数据时遇到问题,JSON文件中的所有对象都被转换为字符串,偶数 这是我从Node.js 保存JSON数据时出错-NodeJS Express,node.js,arrays,json,express,Node.js,Arrays,Json,Express,我在保存和访问JSON数据时遇到问题,JSON文件中的所有对象都被转换为字符串,偶数 这是我从HTML表单 control.post('/like/:id',getuserdata, async (req, res, next) => { try{ let likedata = './bin/like/likedata.json' let title = res.accountuser.title; let pass = res.ac
HTML表单
control.post('/like/:id',getuserdata, async (req, res, next) => {
try{
let likedata = './bin/like/likedata.json'
let title = res.accountuser.title;
let pass = res.accountuser.password;
let tags = req.body.likes;
let actions = req.body.action;
let like = {
username: title,
password: pass,
tag: tags,
times: actions,
}
let jsonlike = JSON.stringify(like)
fs.appendFileSync(likedata, jsonlike)
next();
res.redirect('/dashboard')
} catch (err){
console.log(err)
}
});
这就是我的JSON文件是如何产生的当我添加两次或更多值时,JSON文件显示错误预期的文件结尾。
{"username":"account1","password":"somepass1","tag":"hello","times":"5"}{"username":"account2","password":"somepass2","tag":"helllo","times":"444"}
我想单独使用这个JSON数据,这里:
const ig = require('./like');
var cron = require('node-cron');
const iglike = async() => {
await ig.initialize();
await ig.login(username, password); //here
await ig.liketagsprocess(tag, times); //here
};
cron.schedule('* */4 */12 * * *', function() {
iglike();
});
module.exports = iglike;
如何执行此操作?您在JSON文件中写入的内容根本不是有效的JSON
// Contents of your file
{"username":"account1","password":"somepass1","tag":"hello","times":"5"}{"username":"account2","password":"somepass2","tag":"helllo","times":"444"}
请尝试此结构,它是一个JSON对象数组:
[{"username":"account1","password":"somepass1","tag":"hello","times":"5"}, {"username":"account2","password":"somepass2","tag":"helllo","times":"444"}]
如果您生成此数据结构并以与现在相同的方式将其写入一个JSON
文件中,那么您就不会出现错误
另外,您可能应该尽量减少读/写操作appendFileSync()
是一个阻塞操作,您可以使用require
加载一个有效的JSON
文件,并对其进行处理,定期尝试通过覆盖对象JSON数组的当前状态来更新文件中的内容
将下面的代码复制粘贴到名为app.js
的文件中,并通过键入node app.js
运行,检查console.log()
输出和生成的json
文件。结果不言自明
const like = require('./like.json');
const fs = require('fs');
const init = () => {
console.log(like);
let likedata = [
{
"hello": "world",
"one": 1
},
{
"hi": "there",
"two": 2
}
];
let likedatastr = [
{
"hey": "whatsup",
"three_str": "3"
}
];
let likedatamod = [
{
"hey": likedatastr[0].hey,
"three_int": parseInt(likedatastr[0].three_str)
}
]
fs.writeFileSync('./likedata1.json', likedata);
fs.writeFileSync('./likedata2.json', JSON.stringify(likedata));
fs.writeFileSync('./likedatastr.json', JSON.stringify(likedatastr));
fs.writeFileSync('./likedatamod.json', JSON.stringify(likedatamod));
};
init();
而like.json
如下所示:
[{"username":"account1","password":"somepass1","tag":"hello","times":"5"}, {"username":"account2","password":"somepass2","tag":"helllo","times":"444"]
同样,确保这些数字实际上是从请求主体中提取的数据中的数字。理想情况下,您应该在请求正文数据上添加一个parseInt()
(假设您需要整数),并处理NaN
,以防万一
祝您好运。您正在写入JSON文件的内容根本不是有效的JSON
// Contents of your file
{"username":"account1","password":"somepass1","tag":"hello","times":"5"}{"username":"account2","password":"somepass2","tag":"helllo","times":"444"}
请尝试此结构,它是一个JSON对象数组:
[{"username":"account1","password":"somepass1","tag":"hello","times":"5"}, {"username":"account2","password":"somepass2","tag":"helllo","times":"444"}]
如果您生成此数据结构并以与现在相同的方式将其写入一个JSON
文件中,那么您就不会出现错误
另外,您可能应该尽量减少读/写操作appendFileSync()
是一个阻塞操作,您可以使用require
加载一个有效的JSON
文件,并对其进行处理,定期尝试通过覆盖对象JSON数组的当前状态来更新文件中的内容
将下面的代码复制粘贴到名为app.js
的文件中,并通过键入node app.js
运行,检查console.log()
输出和生成的json
文件。结果不言自明
const like = require('./like.json');
const fs = require('fs');
const init = () => {
console.log(like);
let likedata = [
{
"hello": "world",
"one": 1
},
{
"hi": "there",
"two": 2
}
];
let likedatastr = [
{
"hey": "whatsup",
"three_str": "3"
}
];
let likedatamod = [
{
"hey": likedatastr[0].hey,
"three_int": parseInt(likedatastr[0].three_str)
}
]
fs.writeFileSync('./likedata1.json', likedata);
fs.writeFileSync('./likedata2.json', JSON.stringify(likedata));
fs.writeFileSync('./likedatastr.json', JSON.stringify(likedatastr));
fs.writeFileSync('./likedatamod.json', JSON.stringify(likedatamod));
};
init();
而like.json
如下所示:
[{"username":"account1","password":"somepass1","tag":"hello","times":"5"}, {"username":"account2","password":"somepass2","tag":"helllo","times":"444"]
同样,确保这些数字实际上是从请求主体中提取的数据中的数字。理想情况下,您应该在请求正文数据上添加一个parseInt()
(假设您需要整数),并处理NaN
,以防万一
祝你好运。谢谢你的帮助!谢谢你的帮助!