Categories
JavaScript Answers

How to return certain fields with .populate() from Mongoose?

Sometimes, we want to return certain fields with .populate() from Mongoose.

In this article, we’ll look at how to return certain fields with .populate() from Mongoose.

How to return certain fields with .populate() from Mongoose?

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.

For instance, we write

Model
  .findOne({
    _id
  })
  .populate('field', 'name')

to call populate with 'field' and 'name' to populate the field and name fields in the returned documents.

Conclusion

To return certain fields with .populate() from Mongoose, we can call populate with the fields we want to populate.

Categories
JavaScript Answers

How to render a variable as HTML in EJS?

Sometimes, we want to render a variable as HTML in EJS.

In this article, we’ll look at how to render a variable as HTML in EJS.

How to render a variable as HTML in EJS?

To render a variable as HTML in EJS, we can use various tags.

We write

<% code %>

to run code code which is evaluated but not printed.

We write

<%= code %>

to evaluate and print out code as an escaped string.

And we write

<%- my_form_content %>

to evaluate and print out code as is.

Conclusion

To render a variable as HTML in EJS, we can use various tags.

Categories
JavaScript Answers

How to display Node.js raw Buffer data as Hex string?

Sometimes, we want to display Node.js raw Buffer data as Hex string.

In this article, we’ll look at how to display Node.js raw Buffer data as Hex string.

How to display Node.js raw Buffer data as Hex string?

To display Node.js raw Buffer data as Hex string, we can use the buffer toString method.

For instance, we write

buff.toString('hex');

to convert the buff buffer to a string with toString.

We call toString with 'hex' to return a hex version of the raw buffer data.

Conclusion

To display Node.js raw Buffer data as Hex string, we can use the buffer toString method.

Categories
JavaScript Answers

How to set the Sequelize findAll sort order in Node.js?

Sometimes, we want to set the Sequelize findAll sort order in Node.js.

In this article, we’ll look at how to set the Sequelize findAll sort order in Node.js.

How to set the Sequelize findAll sort order in Node.js?

To set the Sequelize findAll sort order in Node.js, we can set the order property.

For instance, we write

const getStaticCompanies = () => {
  return Company.findAll({
    where: {
      //...
    },
    order: [
      ['id', 'DESC'],
      ['name', 'ASC'],
    ],
    attributes: ['id', 'logo_version', 'logo_content_type', 'name', 'updated_at']
  });
};

to call Company.findAll with an object that has the order property set an array to the columns and how we want to order them.

Conclusion

To set the Sequelize findAll sort order in Node.js, we can set the order property.

Categories
JavaScript Answers

How to use a MongoDB password with “@” in it in Node.js?

Sometimes, we want to use a MongoDB password with "@" in it in Node.js.

In this article, we’ll look at how to use a MongoDB password with "@" in it in Node.js.

How to use a MongoDB password with "@" in it in Node.js?

To use a MongoDB password with "@" in it in Node.js, we should escape the ‘@’ by URL encoding it.

For instance, we write

mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", {
  useNewUrlParser: true
}, (err, db) => {

});

to replace ‘@’ with %40 in our connecting string.

Then we set useNewUrlParser to true so that the escaped ‘@’ is converted back to ‘@’.

Conclusion

To use a MongoDB password with "@" in it in Node.js, we should escape the ‘@’ by URL encoding it.