Categories
JavaScript Answers

How to fix Node.js dotenv isn’t loading environment variables?

Sometimes, we want to fix Node.js dotenv isn’t loading environment variables.

In this article, we’ll look at how to fix Node.js dotenv isn’t loading environment variables.

How to fix Node.js dotenv isn’t loading environment variables?

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

For instance, we write

const path = require('path')
require('dotenv').config({
  path: path.resolve(__dirname, '../.env')
})

to call config with an object with path set to the path of the .env file.

As a result, the .env file’s environment variables will be loaded into process.env.

Conclusion

To fix Node.js dotenv isn’t loading environment variables, we can call config with an object with path set to the path of the config file with the environment variables.

Categories
JavaScript Answers

How to make a query with OR condition with Node.js Sequelize?

Sometimes, we want to make a query with OR condition with Node.js Sequelize.

In this article, we’ll look at how to make a query with OR condition with Node.js Sequelize.

How to make a query with OR condition with Node.js Sequelize?

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.

For instance, w write

const {
  Op
} = require('Sequelize');

const r = await to(Student.findAll({
  where: {
    LastName: "Doe",
    FirstName: {
      [Op.or]: ["John", "Jane"]
    },
    Age: {
      [Op.between]: [18, 24]
    }
  }
}));

to call findAll with the where property set to an object with the fields we’re filtering by.

And we use the Op.or property set to an array of values to search for entries with FirstName set to 'John' or 'Jane'.

We use Op.between to search for entries with Age between 18 and 24.

Each property in where will be joined together with OR.

Conclusion

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.

Categories
JavaScript Answers

How to run query after populate in Mongoose?

Sometimes, we want to run query after populate in Mongoose.

In this article, we’ll look at how to run query after populate in Mongoose.

How to run query after populate in Mongoose?

To run query after populate in Mongoose, we call populate with an object with the query.

For instance, we write

const query = Models.Item.find({});

query
  .desc('dateCreated')
  .populate('tags', null, {
    tagName: {
      $in: ['funny', 'politics']
    }
  })

to call populate with an object that has the field to query as the first argument and an object with the predicate data as the 3rd argument.

Therefore, we query the tags field to find the items with the tagName equal to 'funny' or 'politics' and populate each entry in the results with the query result from calling populate.

Conclusion

To run query after populate in Mongoose, we call populate with an object with the query.

Categories
JavaScript Answers

How to convert relative path to absolute with Node.js?

Sometimes, we want to convert relative path to absolute with Node.js.

In this article, we’ll look at how to convert relative path to absolute with Node.js.

How to convert relative path to absolute with Node.js?

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

For instance, we write

const {
  resolve
} = require('path')

resolve('../../dir/tmp.txt')

to call resolve with a relative path string to return the absolute path equivalent of the relative path as a string.

Conclusion

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

Categories
JavaScript Answers

How to read file as a base64 string with Node.js?

Sometimes, we want to read file as a base64 string with Node.js

In this article, we’ll look at how to read file as a base64 string with Node.js.

How to read file as a base64 string with Node.js?

To read file as a base64 string with Node.js, we can use the readFile method with the encoding option.

For instance, we write

const fs = require('fs').promises;

const read = async () => {
  const contents = await fs.readFile('/path/to/file.jpg', {
    encoding: 'base64'
  });
}

to call fs.readFile with the file path and an object with the encoding property set to 'base64' to read the file as a base64 string.

It returns a promise, so we use await to get the read file contents from the resolved value.

We require the promise version of the fs module with

const fs = require('fs').promises;

Conclusion

To read file as a base64 string with Node.js, we can use the readFile method with the encoding option.