javascript - writeFileSync doesn't callback - Stack Overflow

I am using writeFileSync function to write a file locally, the file does get written, however, the call

I am using writeFileSync function to write a file locally, the file does get written, however, the callback function is never called.

I did some googling, some other post are having the issue that it's either 1) passing the content went wrong or 2) having two write function at the same time.

My problem is that there are some other places in my code that is using the writeFileSync, but they are on different routes (not sure if this is the right terminology, localhost:port#/differentroutes <- something like this). I am testing only on my own route so those write functions shouldn't even be called.

Here is my code:

if(!fs.existsSync(dir)){
        fs.mkdirSync(dir)
    }

//content is just a string var I swear it's just a string

    fs.writeFileSync('./pages/SubmissionProcess.html',content,function(err){
        if(err){
            throw err
        }else {
            console.log("YES")
        }
    })

I never see this "YES" nor error in my console even tho the file is already written....

I am using writeFileSync function to write a file locally, the file does get written, however, the callback function is never called.

I did some googling, some other post are having the issue that it's either 1) passing the content went wrong or 2) having two write function at the same time.

My problem is that there are some other places in my code that is using the writeFileSync, but they are on different routes (not sure if this is the right terminology, localhost:port#/differentroutes <- something like this). I am testing only on my own route so those write functions shouldn't even be called.

Here is my code:

if(!fs.existsSync(dir)){
        fs.mkdirSync(dir)
    }

//content is just a string var I swear it's just a string

    fs.writeFileSync('./pages/SubmissionProcess.html',content,function(err){
        if(err){
            throw err
        }else {
            console.log("YES")
        }
    })

I never see this "YES" nor error in my console even tho the file is already written....

Share Improve this question asked Oct 5, 2018 at 18:10 AnnaAnna 5719 silver badges31 bronze badges 9
  • Cos it's sync duh, dont need to register a callback!!!! – shanks Commented Oct 5, 2018 at 18:12
  • @shanks what does that mean... – Anna Commented Oct 5, 2018 at 18:12
  • You only register callbacks for async functions, sync versions dont need callbacks – shanks Commented Oct 5, 2018 at 18:13
  • fs.writeFileSync('./pages/SubmissionProcess.html',content) should surffice – shanks Commented Oct 5, 2018 at 18:14
  • @shanks okay...so how do I know it finished??? – Anna Commented Oct 5, 2018 at 18:14
 |  Show 4 more ments

2 Answers 2

Reset to default 3

Write file sync doesn't take a callback :D

Take a look at the documentation :

https://nodejs/api/fs.html#fs_fs_writefilesync_file_data_options

The parameters are (path, data, options)

If you want to check if the file actually wrote, you can read file sync after writing it or check the last time the file was updated. Otherwise, you should try using the async approach.

All of the synchronous methods throw rather than passing an error to a callback.

try {
    fs.writeFileSync('./pages/SubmissionProcess.html', content);
    console.log('YES');
} catch (e) {
    console.error(e);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633827a4637266.html

相关推荐

  • javascript - writeFileSync doesn&#39;t callback - Stack Overflow

    I am using writeFileSync function to write a file locally, the file does get written, however, the call

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信