DEV Community

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

Collapse
 
jornvm profile image
Jornvm

Great writing for educational purposes! Going trough this was my first encounter with both node.js, backend and using wsl 2. I got stuck on the last part, console returns my input but does not create the user_mood.txt.
I also get a DeprecationWarning for calling an asynchrounous function without callback, so i guess im going to be spending my saturday figuring that out :)

Thank you for this guide :D

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!! :)

Collapse
 
lisahjung profile image
Lisa Jung

You are so welcome Jornvm. I am just getting started on Node.js myself and it's so exciting to hear that you are delving into it as well.

Hm... I haven't encountered that issue while I was building the server. Have you tried comparing your code to the my GitHub repo for this tutorial? github.com/LisaHJung/Node.js_Tutor...

You probably figured it out already lol but let me know how it went!