Advertisement

JavaScript/Node.js Question

Started by November 22, 2018 02:01 PM
0 comments, last by JensB 6 years ago

I'm working a Node.js class in Udemy, and the instructor runs this code:


var fs = require('fs')

var data = {
    name: 'Bob'
}

fs.writeFile('data.json', data)

For him, this produces a data.json file (with inproper data, that's the next step) - However, for me, I get an exception:

Quote

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

As far as I can think, it expects "data" to be a callback - But when I look at the docs, writeFile has a version that takes filename, data with no callback - What am I missing here? 

Class is a few versions back on Node.js, I'm running most current - 11.2.0

 

EDIT: Figured it out...

Never mind, if I had listened for 2 more minutes I'd gotten the answer - Just wasted 10 mins on this ;)

He actually got a warning that using it without a callback is deprecated functionality, and I guess it's finally gone now - And I must have misread the docs.

Here is the working code, leaving it here if someone else stumbles into this:


var fs = require('fs')

var data = {
    name: 'Bob'
}

fs.writeFile('data.json', JSON.stringify(data),(err) => {
    console.log("Write finished! ", err)
})

 

// Jens

This topic is closed to new replies.

Advertisement