Categories
JavaScript Answers

How to fix Error: ENOENT, stat ‘./path/to/file’ error in Node.js?

To fix Error: ENOENT, stat ‘./path/to/file’ error in Node.js, we need to use the __dirname variable to access an absolute path in the project.

For instance, we write

const path = require("path");

const fullPath = path.join(__dirname, "path/to/file");

to call path.join to combine the project directory __dirname with the relative path in the project folder we want to access.

join returns the full path as a string.

Categories
JavaScript Answers

How to do MongoDB atomic “findOrCreate”: findOne, insert if nonexistent, but do not update with JavaScript?

To do MongoDB atomic "findOrCreate": findOne, insert if nonexistent, but do not update with JavaScript, we use the findAndModify method.

For instance, we write

db.collection.findAndModify({
  query: { _id: "id" },
  update: {
    $setOnInsert: { foo: "bar" },
  },
  new: true,
  upsert: true,
});

to call findAndModify with an object with the query to look for the item with the ID 'id'.

We set update to the field on update.

upsert is set to true to insert a new entry if the item doesn’t exist.

Categories
JavaScript Answers

How to set up environment specific configs to be used with everyauth with Node.js?

To set up environment specific configs to be used with everyauth with Node.js, we return the config according to the NODE_ENV environment variable.

For instance, we write

module.exports = () => {
  switch (process.env.NODE_ENV) {
    case "development":
      return {
        //...
      };
    case "production":
      return {
        //...
      };
    default:
      return {
        //...
      };
  }
};

in the config.js file to return the config according to the NODE_ENV environment variable value.

Then we write

const Config = require("./config");
const conf = Config();

to require the config.js file and then call the imported Config function to return the config for the environment in app.js.

And then we run

NODE_ENV=production node app.js

to set the NODE_ENV environment variable value and run app.js.

Categories
JavaScript Answers

How to add Links inside a paragraph with Jade and JavaScript?

To add Links inside a paragraph with Jade and JavaScript, we can add the a element with the Jade syntax.

For instance, we write

p: #[span this is the start] #[a(href="http://example.com") a link] #[span and this is the rest of the paragraph]

to add the link with

#[a(href="http://example.com") a link] #[span and this is the rest of the paragraph]

The attributes are in the parentheses.

The span is nested in the link.

Categories
JavaScript Answers

How to pass variable from jade template file to a script file with JavaScript?

To pass variable from jade template file to a script file with JavaScript, we pass in the variable to the template with render.

For instance, in our Jade template, we write

script.
  loginName="#{login}";

Then we write

exports.index = (req, res) => {
  res.render("index", { layout: false, login: req.session.login });
};

to call res.render with an object with the login property set to the value we want to pass to the Jade template.