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.

Categories
JavaScript Answers

How to upload images using Node.js, Express, and Mongoose?

Sometimes, we want to upload images using Node.js, Express, and Mongoose.

In this article, we’ll look at how to upload images using Node.js, Express, and Mongoose.

How to upload images using Node.js, Express, and Mongoose?

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

To use it, we write

app.use(express.bodyParser());

app.post('/todo/create', (req, res) => {
  res.send(console.dir(req.files));
});

to call express.bodyParser to return a middleware to parse multipart form data request bodies.

And then we call app.use to use the middleware.

Then we create a POST route with app.post.

And we get the uploaded files from req.files in the route handler.

Then in the template, we write

form(action="/todo/create", method="POST", enctype="multipart/form-data")
    input(type='file', name='todo')
    button(type='submit') New

to add a form element with the action attribute set to /todo/create to submit our form data to the route we just created.

We set enctype to multipart/form-data to make it submit form data.

In the form element, we add a file input to let us accept files.

Conclusion

To upload images using Node.js, Express, and Mongoose, we canm use the bodyParser middleware.

Categories
JavaScript Answers

How to make requests in sequential order Node.js?

Sometimes, we want to make requests in sequential order Node.js.

In this article, we’ll look at how to make requests in sequential order Node.js.

How to make requests in sequential order Node.js?

To make requests in sequential order Node.js, we can use axios.

To install it, we run

npm i axios

Then we write

const axios = require('axios')

const makeRequests = async () => {
  const {
    data
  } = await axios.get(url1)
  const {
    data: data2
  } = await axios.get(url2)
  const {
    data: data3
  } = await axios.get(url3)
}

to call axios.get to make GET requests to 3 different URLs url1, url2, and url3.

And we get the response from the data property of the resolved value of each promise.

await returns the resolve value of the promise.

Conclusion

To make requests in sequential order Node.js, we can use axios.

Categories
JavaScript Answers

How to delete array element in document and save with Mongoose?

Sometimes, we want to delete array element in document and save with Mongoose.

In this article, we’ll look at how to delete array element in document and save with Mongoose.

How to delete array element in document and save with Mongoose?

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.

For instance, we write

Favorite.updateOne({
  name
}, {
  $pullAll: {
    favorites: req.params.deleteUid,
  },
});

to call updateOne with an object to search for the document with the name value set to name.

The 2nd object has the $pullAll property which lets us remove the favorites entries in the selected Favorite document with the _id set to req.params.deleteUid.

Conclusion

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.