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.

Categories
JavaScript Answers

How to replace callbacks with promises in Node.js?

Sometimes, we want to replace callbacks with promises in Node.js.

In this article, we’ll look at how to replace callbacks with promises in Node.js.

How to replace callbacks with promises in Node.js?

To replace callbacks with promises in Node.js, we can use the util.promisify method.

For instance, we write

import mysql from 'mysql';
const util = require('util');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'db'
});

const connectAsync = util.promisify(connection.connectAsync);
const queryAsync = util.promisify(connection.queryAsync);

const getUsersAsync = async () => {
  await connectAsync()
  return queryAsync('SELECT * FROM Users')
};

to convert the connection.connectAsync and connection.queryAstnc methods to functions that returns promises with util.promisify.

Then in the getUsersAsync function, we call them both.

We use await to wait for the promise returned by connectAsync to resolve before calling queryAsync.

Conclusion

To replace callbacks with promises in Node.js, we can use the util.promisify method.

Categories
JavaScript Answers

How to get data out of a Node.js HTTP get request?

Sometimes, we want to get data out of a Node.js HTTP get request.

In this article, we’ll look at how to get data out of a Node.js HTTP get request.

How to get data out of a Node.js HTTP get request?

To get data out of a Node.js HTTP get request, we can get the data from the data event callback.

For instance, we write

const http = require('http')

http.get(options, (response) => {
  response.setEncoding('utf8')
  response.on('data', console.log)
  response.on('error', console.error)
})

to call http.get to make a HTTP GET request.

We get the request response from the 'data' event payload.

To get the data from the payload, we call response.on with 'data' and a callback that has the response data in the first parameter.

Therefore, when the 'data' event callback is run, we see the response data logged with console.log.

Conclusion

To get data out of a Node.js HTTP get request, we can get the data from the data event callback.