Welcome to the Linux Foundation Forum!

Errata in code in LFW211 Ch. 13, File System Reading & Writing, Callback example

Hello,

In the Node.JS Applications course, LFW211, in Ch. 13 on the File System (fs) on the Reading and Writing page, there is an errata in the code examples.

It's in the first callback example using Node.JS fs.readFile(). The code you have is —

'use strict'
const { readFile } = require('fs')
readFile(__filename, {encoding: 'utf8'}, (err, contents) => {
  if (err) {
    console.error(err)
    return
  }
})

This will output nothing, it only error checks, so there'd have to be a system erorr, like the previous permissions on the file error shown on the same page, for the error to throw and give that output. What I believe you wanted was —

'use strict'
const { readFile } = require('fs')
readFile(__filename, {encoding: 'utf8'}, (err, contents) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(contents)
})

This outputs the correct result. You have it correct in what the file is supposed to output in the image you have after that —

Image of Code Result for Reading and Writing to the Node.JS File System with a Callback

...however, I still think you want to correct the code example also.

best,

faddah

Comments

Categories

Upcoming Training