【问题标题】:Data not saving to json?数据不保存到json?
【发布时间】:2021-08-01 13:12:11
【问题描述】:

我的代码是这样的:

if (y[1] == "suggestion_channel") {
    saveJson.servers[guild.id].suggestChannel = message.mentions.channels.first().id;
    message.channel.send("Set suggest channel to: <#" + saveJson.servers[guild.id].suggestChannel + ">");
}

var save = JSON.stringify(saveJson);
fs.writeFileSync('./data.json', save);

现在一切正常,但唯一不起作用的是保存部分....可以找到完整的代码Here 请好心,因为我已经尝试修复这个问题了,谢谢! :D

更新: 只使用 fs.writeFile 不同步!

【问题讨论】:

    标签: javascript node.js json discord.js


    【解决方案1】:

    fs.writeFileSync() 接受一个回调函数,可以使用它看看是否有错误。

    试试这样的

    fs.writeFileSync('./data.json', save,function(err) {
        if (err) {
            console.log(err);
        }
    });

    【讨论】:

    • 您是否在控制台记录了 save 以查看它是否实际保存了您要保存在文件中的数据?
    • 是的,所有新旧数据都在保存变量中
    • fs.writeFileSync() 没有回调请参阅 NodeJS 文档nodejs.org/dist/latest-v16.x/docs/api/…
    • 我也使用 fs.writeFile 修复了它!感谢您尝试帮助:D
    • 我的错,是的,@VictorPerez 是对的 fs.writeFileSync() 没有回调,但 fs.writeFile 有。
    猜你喜欢
    • 2021-05-05
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多