简体   繁体   中英

Filesystem doesn't want to write a specific file in Node.js

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.

I have no idea what is going on, I have checked for spelling multiple times, but everything seems like it should work, but doesn't.

additional information: node -v // v14.2.0
windows 10

btw:

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

in case that wasn't obvious.

Edit: This is more like the thing I am trying to make (of course, minimum needed code to show the issue).

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

Edit 2:

More context:

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)
 }
}

Something like that?

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

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

fs.appendFile() will create a file if not existing, or rewrite an ewisting one. I used callbacks here, but you can also use synchronous functions of fs , instead of async ones, like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM