Categories
JavaScript Answers

How to populate nested array in Mongoose?

Sometimes, we want to populate nested array in Mongoose.

In this article, we’ll look at how to populate nested array in Mongoose.

How to populate nested array in Mongoose?

To populate nested array in Mongoose, we can use the populate method.

For instance, we write

Project.find(query)
  .populate({
    path: 'pages',
    populate: {
      path: 'components',
      model: 'Component'
    }
  })
  .exec((err, docs) => {});

to call populate with an object with the array property we want to populate in the docs result.

We populate pages with the components entries in the returned Project entries.

docs has the returned result with the nested array results inside each entry.

Conclusion

To populate nested array in Mongoose, we can use the populate method.

Categories
JavaScript Answers

How to execute and get the output of a shell command in Node.js?

Sometimes, we want to execute and get the output of a shell command in Node.js.

In this article, we’ll look at how to execute and get the output of a shell command in Node.js.

How to execute and get the output of a shell command in Node.js?

To execute and get the output of a shell command in Node.js, we can use the child_process module’s exec method.

For instance, we write

const {
  exec
} = require('child_process');

exec(command, (error, stdout, stderr) => {
  console.log(error, stdout, stderr)
});

to call exec with the command string we want to run.

The 2nd argument is a callback that has the stdout and stderr output after running the command.

error has the error that’s thrown when the command is run.

Conclusion

To execute and get the output of a shell command in Node.js, we can use the child_process module’s exec method.

Categories
JavaScript Answers

How to use an http proxy with Node.js http.Client?

Sometimes, we want to use an http proxy with Node.js http.Client.

In this article, we’ll look at how to use an http proxy with Node.js http.Client.

How to use an http proxy with Node.js http.Client?

To use an http proxy with Node.js http.Client, we can use the http.get method to connect to the proxy.

For instance, we write

const http = require("http");

const options = {
  host: "proxy",
  port: 8080,
  path: "http://www.google.com",
  headers: {
    Host: "www.google.com"
  }
};

http.get(options, (res) => {
  console.log(res);
  res.pipe(process.stdout);
});

to call http.get with the options for the connection.

We set the host to the proxy URL.

path has the path that we want to proxy with the proxy.

And headers has the Host with the host URL to proxy.

In the get callback, we get the response from res.

Conclusion

To use an http proxy with Node.js http.Client, we can use the http.get method to connect to the proxy.

Categories
JavaScript Answers

How to fix the ‘db.collection is not a function’ error when using MongoClient v3.0?

Sometimes, we want to fix the ‘db.collection is not a function’ error when using MongoClient v3.0.

In this article, we’ll look at how to fix the ‘db.collection is not a function’ error when using MongoClient v3.0.

How to fix the ‘db.collection is not a function’ error when using MongoClient v3.0?

To fix the ‘db.collection is not a function’ error when using MongoClient v3.0, we should use the client.db method to return the database handle.

For instance, we write

MongoClient.connect('mongodb://localhost', (err, client) => {
  if (err) {
    throw err;
  }

  const db = client.db('mytestingdb');

  db.collection('customers').findOne({}, (findErr, result) => {
    if (findErr) {
      throw findErr;
    }
    console.log(result.name);
    client.close();
  });
});

to call MongoClient.connect to connect to our database.

In the callback we pass into connect, we get the client object, which we use to get the db handle with the client.db method.

Then we call db.collection to query the 'customers' collection.

The query results are in result.

Finally, we call client.close to close the database connection when we’re done.

Conclusion

To fix the ‘db.collection is not a function’ error when using MongoClient v3.0, we should use the client.db method to return the database handle.

Categories
JavaScript Answers

How to use multiple database in a single Node.js project with Mongoose?

Sometimes, we want to use multiple database in a single Node.js project with Mongoose.

In this article, we’ll look at how to use multiple database in a single Node.js project with Mongoose.

How to use multiple database in a single Node.js project with Mongoose?

To use multiple database in a single Node.js project with Mongoose, we can use the connection’s model method.

For instance, we write

const conn = mongoose.createConnection('mongodb://localhost/testA');
const conn2 = mongoose.createConnection('mongodb://localhost/testB');

const ModelA = conn.model('Model', new mongoose.Schema({
  title: {
    type: String,
    default: 'model in testA database'
  }
}));

const ModelB = conn2.model('Model', new mongoose.Schema({
  title: {
    type: String,
    default: 'model in testB database'
  }
}));

to call mongoose.createConnection to connect to 2 databases.

Then we call model on each to create 2 models from 2 different connections and 2 different schemas.

We add the title property to each schema with type String.

Conclusion

To use multiple database in a single Node.js project with Mongoose, we can use the connection’s model method.