Categories
JavaScript Answers

How to protect the password field in MongoDB Mongoose so it won’t return in a query?

Sometimes, we want to protect the password field in MongoDB Mongoose so it won’t return in a query.

In this article, we’ll look at how to protect the password field in MongoDB Mongoose so it won’t return in a query.

How to protect the password field in MongoDB Mongoose so it won’t return in a query?

To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.

For instance, we write

const userSchema = new Schema({
  name: {
    type: String,
    required: false,
    minlength: 5
  },
  email: {
    type: String,
    required: true,
    minlength: 5
  },
  phone: String,
  password: {
    type: String,
    select: false
  },
})

to create the userSchema that has the password field set to type String.

And we add select and set it to false to prevent it from being returned in queries.

Conclusion

To protect the password field in MongoDB Mongoose so it won’t return in a query, we can set the select option to false on a field when we create the schema.

Categories
JavaScript Answers

How to modify the data returned by a Mongoose query in Node.js?

Sometimes, we want to modify the data returned by a Mongoose query in Node.js.

In this article, we’ll look at how to modify the data returned by a Mongoose query in Node.js.

How to modify the data returned by a Mongoose query in Node.js?

To modify the data returned by a Mongoose query in Node.js, we can call lean to make Mongoose return a plain object version of the query result that we can modify.

For instance, we write

Survey.findById(req.params.id).lean().exec((err, data) => {
  data.surveyQuestions.forEach((sq) => {
    Question.findById(sq.question, (err, q) => {
      sq.question = q;
    });
  });
});

to call lean after call findById to make the data in the exec callback a plain JavaScript object version of the query result.

We then loop through each item in data.surveyQuestions and set sq.question to q on each entry in the Question.findById callback.

Conclusion

To modify the data returned by a Mongoose query in Node.js, we can call lean to make Mongoose return a plain object version of the query result that we can modify.

Categories
JavaScript Answers

How to add connection pooling with Node.js and MySQL?

Sometimes, we want to add connection pooling with Node.js and MySQL.

In this article, we’ll look at how to add connection pooling with Node.js and MySQL.

How to add connection pooling with Node.js and MySQL?

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.

For instance, we write

const getConnection = (callback) => {
  pool.getConnection((err, connection) => {
    callback(err, connection);
  });
};

to define the getConnection function that takes the callback we want to call after the pool.getConnection is done.

We get the MySQL connection from the connection parameter.

And we call callback with the connection so we can use it elsewhere.

Then when we’re done, we call connection.release(); to release the connection.

Conclusion

To add connection pooling with Node.js and MySQL, we call pool.getConnection to get a connection.

Categories
JavaScript Answers

How to auto generate migrations with Sequelize CLI from Sequelize models?

Sometimes, we want to auto generate migrations with Sequelize CLI from Sequelize models

In this article, we’ll look at how to auto generate migrations with Sequelize CLI from Sequelize models.

How to auto generate migrations with Sequelize CLI from Sequelize models?

To auto generate migrations with Sequelize CLI from Sequelize models, we can run sequelize migration:generate.

For instance, we run

sequelize migration:generate --name [name_of_migration]

to run sequelize migration:generate with the --name option followed by the name of the migration we want to create to create a blank migration file.

The model data has to be created manually.

Conclusion

To auto generate migrations with Sequelize CLI from Sequelize models, we can run sequelize migration:generate.

Categories
JavaScript Answers

How to get city name from a latitude and longitude point with Node.js?

Sometimes, we want to get city name from a latitude and longitude point with Node.js.

In this article, we’ll look at how to get city name from a latitude and longitude point with Node.js.

How to get city name from a latitude and longitude point with Node.js?

To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder package.

To install it, we run

npm i node-geocder

Then we use it by writing

const NodeGeocoder = require('node-geocoder');

const options = {
  provider: 'google',
  httpAdapter: 'https',
  apiKey: '...',
  formatter: 'json'
};

const geocoder = NodeGeocoder(options);

geocoder.reverse({
  lat: 49.5967439,
  lon: 77.3285038
}, (err, res) => {
  console.log(res);
});

We create a NodeGeocoder instance with some options to specify the map provider, API key, etc, and the format of the data with the formatter property.

Then we call geocoder.reverse with an object that has the lat latitude and lon longitude.

The callback’s res parameter has the location data for the given coordinates.

Conclusion

To get city name from a latitude and longitude point with Node.js, we can use the node-geocoder package.