Categories
JavaScript Answers

How to fix “ERROR: dbpath (/data/db) does not exist.” with Node MongoDB?

To fix "ERROR: dbpath (/data/db) does not exist." with Node MongoDB, we should make sure the /data/db folder exists.

To do this, we run

sudo mkdir -p /data/db/

to create the /data/db/ folder.

Then we set the current user as the owner of the folder with

sudo chown `id -u` /data/db
Categories
JavaScript Answers

How to get the latest and oldest record in Node Mongoose.js?

To get the latest and oldest record in Node Mongoose.js, we use the sort method.

For instance, we write

Tweet.findOne()
  .sort("-created_at")
  .exec((err, post) => {
    //...
  });

to call sort to sort the results by the created_at field in descending order.

Then we get the latest result from post in the callback.

Categories
JavaScript Answers

How to find Cannot find module ‘bcrypt’ error with Node?

To find Cannot find module ‘bcrypt’ error with Node, we install a few packages.

To fix this, we run

npm install node-gyp -g
npm install bcrypt -g

npm install bcrypt --save

to install node-gyp and bcrypt globally.

Then we run

npm install bcrypt --save

to install bcrypt locally.

Categories
JavaScript Answers

How to add line breaks in Node Jade?

To add line breaks in Node Jade, we add a br element.

For instance, we write

p.
    Some text on the first line.#[br]
    Some text on the second line.

to add the br element with #[br].

Categories
JavaScript Answers

How to prevent XSS in Node.js?

To prevent XSS in Node.js, we use the validator module.

For instance, we write

const validator = require("validator");

const escapedString = validator.escape(someString);

to call validator.escape with someString to return an escaped version of someString.