DEV Community

Discussion on: Beginner's guide to creating a Node.js server

Collapse
 
crispy1260 profile image
Christopher Payne

Hey Jornvm,

When I was running through this, I got the following error in my console referencing the fs.writeFile section.

mood=Excited
fs.js:144
  throw new ERR_INVALID_CALLBACK(cb);
  ^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at maybeCallback (fs.js:144:9)
    at Object.writeFile (fs.js:1325:14)
    at IncomingMessage.<anonymous> (E:\Users\Christopher\Documents\GitHub\All_The_Feels\routes.js:30:10)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1221:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'ERR_INVALID_CALLBACK'
}

In my troubleshooting, I added some error handling and it started working for me. I don't understand why adding error handling to that function helped but it allowed the file to be created.

Looking at Lisa's Github repo (linked below), my line 24 of routes.js now looks like:

fs.writeFile("user_mood.txt", mood, function (err) {
    if (err) {
        return console.log(err);
    }
    console.log("File saved successfully!");
});

Hopefully, this gets it to work for you too! I assume it's a copy/paste fail on my part and I am overlooking the error in my file but this worked for me.

Collapse
 
lisahjung profile image
Lisa Jung

Hey Christopher!
Thank you so much for taking the time to share your troubleshooting tips with everyone. I appreciate you!!

Collapse
 
malanius profile image
Malanius Privierre

Hi, this probably depends on the version of Node the post was written and version of Node you're using. The callback parameter is required since Node.js v10.0.0, see docs history where this is mentioned: nodejs.org/api/fs.html#fs_fs_write...

Thread Thread
 
lisahjung profile image
Lisa Jung

Malanius! Thank you for sharing your insights with everyone. One of the many things I love about the developer community is how generous and invested they are in other developers' growth. It feels really good to see readers helping other readers!! :)