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.

Categories
JavaScript Answers

How to pass parameters to a promise function with JavaScript?

Sometimes, we want to pass parameters to a promise function with JavaScript.

In this article, we’ll look at how to pass parameters to a promise function with JavaScript.

How to pass parameters to a promise function with JavaScript?

To pass parameters to a promise function with JavaScript, we can create a function that takes the parameters we want to returns the promise.

For instance, we write

const f = (username, password) => {
  return new Promise((resolve, reject) => {
    //...
    if (success) {
      resolve("Stuff worked!");
    } else {
      reject(Error("It broke"));
    }
  });
}
f('username', 'password')

to create the f function that takes the username and password.

In it, we return a promise created by the Promise constructor.

The callback that we pass into Promise does something with username and password and call resolve if success is true and reject otherwise to reject the promise.

Then we call f with the arguments we want.

Conclusion

To pass parameters to a promise function with JavaScript, we can create a function that takes the parameters we want to returns the promise.

Categories
JavaScript Answers

How to get Express to output nicely formatted HTML?

Sometimes, we want to get Express to output nicely formatted HTML.

In this article, we’ll look at how to get Express to output nicely formatted HTML.

How to get Express to output nicely formatted HTML?

To get Express to output nicely formatted HTML, we set the app.locals.pretty option to true.

For instance, we write

if (app.get('env') === 'development') {
  app.locals.pretty = true;
}

to set app.locals.pretty to true when our Express app is run in the dev environment.

Conclusion

To get Express to output nicely formatted HTML, we set the app.locals.pretty option to true.