Categories
JavaScript Answers

How to avoid deep nesting of asynchronous functions in Node.js?

Sometimes, we want to avoid deep nesting of asynchronous functions in Node.js.

In this article, we’ll look at how to avoid deep nesting of asynchronous functions in Node.js.

How to avoid deep nesting of asynchronous functions in Node.js?

To avoid deep nesting of asynchronous functions in Node.js, we can use the async.waterfall method.

We install the async package with

npm i async

Then we can use it by writing

const async = require("async");


async.waterfall([
  (callback) => {
    callback(null, 'one', 'two');
  },
  (arg1, arg2, callback) => {
    callback(null, 'three');
  },
  (arg1, callback) => {
    // arg1 now equals 'three'
    callback(null, 'done');
  }
], (err, result) => {
  // result now equals 'done'    
});

to call async.waterfall with an array of callback functions.

In each function, we call callback with the arguments we want so async.waterfall will proceed to calling the next callback in the array.

And when all the callbacks are called or if there’s an error, the callback in the 2nd argument is called.

Conclusion

To avoid deep nesting of asynchronous functions in Node.js, we can use the async.waterfall method.

Categories
JavaScript Answers

How to call Node.js fs.readFileSync with a relative path?

Sometimes, we want to call Node.js fs.readFileSync with a relative path.

In this article, we’ll look at how to call Node.js fs.readFileSync with a relative path.

How to call Node.js fs.readFileSync with a relative path?

To call Node.js fs.readFileSync with a relative path, we can call path.resolve with __dirname combined with the relative path.

For instance, we write

const path = require("path");
const file = fs.readFileSync(path.resolve(__dirname, "../file.xml"));

to call fs.readFileSync with the absolute path created by calling path.resolve with __dirname and '../file.xml'.

This will return the absolute path to file.xml which we can use to call readFileSync.

Conclusion

To call Node.js fs.readFileSync with a relative path, we can call path.resolve with __dirname combined with the relative path.

Categories
JavaScript Answers

How to sort a collection by date in MongoDB?

Sometimes, we want to sort a collection by date in MongoDB.

In this article, we’ll look at how to sort a collection by date in MongoDB.

How to sort a collection by date in MongoDB?

To sort a collection by date in MongoDB, we can use the sort method.

For instance, we write

collection.find().sort({
  datefield: -1
}, (err, cursor) => {
  //...
});

to call sort to sort dateField in descending order by setting dateField to -1.

Conclusion

To sort a collection by date in MongoDB, we can use the sort method.

Categories
JavaScript Answers

How to use __dirname in Node.js when using ES6 modules?

Sometimes, we want to use __dirname in Node.js when using ES6 modules.

In this article, we’ll look at how to use __dirname in Node.js when using ES6 modules.

How to use __dirname in Node.js when using ES6 modules?

To use __dirname in Node.js when using ES6 modules, we can use the path module’s dirname function.

For instance, we write

import {
  dirname
} from 'path';
import {
  fileURLToPath
} from 'url';

const __dirname = dirname(fileURLToPath(
  import.meta.url));

to call dirname with the import.meta.url to convert to a path in the Node.js project.

import.meta.url has the URL of the current script.

and we use fileURLToPath to convert the URL to a path.

Conclusion

To use __dirname in Node.js when using ES6 modules, we can use the path module’s dirname function.

Categories
JavaScript Answers

How to capture no file error with Node.js fs.readFileSync()?

Sometimes, we want to capture no file error with Node.js fs.readFileSync().

In this article, we’ll look at how to capture no file error with Node.js fs.readFileSync().

How to capture no file error with Node.js fs.readFileSync()?

To capture no file error with Node.js fs.readFileSync(), we can use try-catch and check for the no file error in the catch with err.code.

For instance, we write

let fileContents;
try {
  fileContents = fs.readFileSync('foo.bar');
} catch (err) {
  if (err.code === 'ENOENT') {
    console.log('File not found!');
  } else {
    throw err;
  }
}

to check if err.code is 'ENOENT'.

If it is, then we know readFileSync threw an error because the 'foo.bar' file wasn’t found.

Conclusion

To capture no file error with Node.js fs.readFileSync(), we can use try-catch and check for the no file error in the catch with err.code.