文件系统不想在Node.js中写入特定文件

问题描述 投票:1回答:1
console.log(`./settings/${mob.serverName}/${mob.channelName}.json`); // ./settings/Mobbot Test Server/mobtimusprime.json
fsp.writeFile(`./settings/${mob.serverName}/newfile.json`, settings); // actually creates the right file, in the right path
console.log(fs.existsSync(`./settings/${mob.serverName}`)); // true
fsp.writeFile(`./settings/${mob.serverName}/${mob.channelName}.json`, settings); // does nothing, doesn't throw an error, just does absolutely nothing.

我不知道发生了什么,我已经检查了多次拼写,但是一切似乎都应该有效,但是没有。

附加信息:节点-v // v14.2.0Windows 10

btw:

const fsp = require('fs').promises;

如果不太明显。

编辑:这更像是我想做的事情(当然,显示问题所需的最少代码)。

const myFunc = async (settings) => {
  try {
    return await fsp.writeFile(`./settings/${mob.serverName}/${mob.channelName}.json`, settings);
  } catch (e) {
    console.log(e)
  }
}

编辑2:

更多上下文:

const settingsUpdater = async (settings) => {
  try {
    return await fsp.writeFile(`./settings/${mob.serverName}/${mob.channelName}.json`, settings);
  } catch (e) {
    console.log(e)
  }
}

const callingFunction = async (...args) {
  try {
    await settingsUpdater(settings);
    const { pieceOfUpdatedSettings } = require(`../settings/${mob.serverName}/${mob.channelName}.json`);
    anotherFunction(pieceOfUpdatedSettings); 
  } catch (e) {
    console.log(e)
 }
}
javascript node.js fs
1个回答
0
投票

类似的东西?

const fs = require('fs');
const path = './path/to/file'
const data = 'yourdata'

fs.appendFile(path, data, (err) => {
    if err console.log(err);
});

fs.appendFile()将创建一个不存在的文件,或重写一个不存在的文件。我在这里使用了回调,但是您也可以使用fs的同步函数,而不是异步的函数,如下所示:

try {
    fs.appendFileSync(path, data);
} catch (err) {
    console.log(err);
}

Fs doc:https://nodejs.org/api/fs.html

© www.soinside.com 2019 - 2024. All rights reserved.