【问题标题】:Errors saving JSON data - NodeJS Express保存 JSON 数据时出错 - NodeJS Express
【发布时间】:2020-08-26 17:26:02
【问题描述】:

我在保存和访问我的 JSON 数据时遇到问题,JSON 文件中的所有对象都被转换为 字符串,偶数。

这是我从HTML FORMGET 数据的快速路线

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 文件显示错误 End of file expected.

{"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;

我该怎么做?

【问题讨论】:

    标签: node.js arrays json express


    【解决方案1】:

    您写入 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 以防万一!

    祝你好运。

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2022-01-26
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2018-03-07
    • 2018-06-11
    • 2020-12-31
    相关资源
    最近更新 更多