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.

Categories
JavaScript Answers Vue Answers

How to use environment variables with Vue.js?

Sometimes, we want to use environment variables with Vue.js.

In this article, we’ll look at how to use environment variables with Vue.js.

How to use environment variables with Vue.js?

To use environment variables with Vue.js, we can add an .env file with the variable keys starting with VUE_APP.

For instance, we write

VUE_APP_MY_ENV_VARIABLE=value
VUE_APP_ANOTHER_VARIABLE=value

Then we can access them in our app with

process.env.VUE_APP_MY_ENV_VARIABLE
process.env.VUE_APP_ANOTHER_VARIABLE

This works if the app is created with Vue CLI.

We can specify .env files with different extensions for different environments.

For instance, we can have

.env                # loaded in all cases
.env.local          # loaded in all cases, ignored by git
.env.[mode]         # only loaded in specified mode
.env.[mode].local   # only loaded in specified mode, ignored by git

Then we can use npm run serve --mode=[mode] to run the Vue project with the given config.

Conclusion

To use environment variables with Vue.js, we can add an .env file with the variable keys starting with VUE_APP.

Categories
JavaScript Answers

How to create a schema from a preexisting collection with Mongoose?

Sometimes, we want to create a schema from a preexisting collection with Mongoose.

In this article, we’ll look at how to create a schema from a preexisting collection with Mongoose.

How to create a schema from a preexisting collection with Mongoose?

To create a schema from a preexisting collection with Mongoose, we can set the collection option when we’re creating the schema.

For instance, we write

new Schema({
  url: String,
  text: String,
  id: Number
}, {
  collection: 'question'
});

to call the Schema constructor with an object with the properties of the documents and another object that specifies the collection` that the schema references.

We reference the question collection with the schema we created.

Conclusion

To create a schema from a preexisting collection with Mongoose, we can set the collection option when we’re creating the schema.

Categories
JavaScript Answers

How to bulk insert in MySQL using Node.js?

Sometimes, we want to bulk insert in MySQL using Node.js.

In this article, we’ll look at how to bulk insert in MySQL using Node.js.

How to bulk insert in MySQL using Node.js?

To bulk insert in MySQL using Node.js, we can use the connection’s query method.

For instance, we write

const mysql = require('mysql');
const conn = mysql.createConnection({
  //...
});

const sql = "INSERT INTO Test (name, email, n) VALUES ?";
const values = [
  ['demian', 'jane@gmail.com', 1],
  ['john', 'mary@gmail.com', 2],
  ['mark', 'alex@gmail.com', 3],
  ['pete', 'pete@gmail.com', 4]
];
conn.query(sql, [values], (err) => {
  if (err) throw err;
  conn.end();
});

to create the DB connection with mysql.createConnection.

Then we call conn.query with the sql and the values in a nested array to insert them all into the SQL.

The ? in the sql will be interpolated with the values to insert.

Then we call conn.end to end the DB connection once the data are inserted.

Conclusion

To bulk insert in MySQL using Node.js, we can use the connection’s query method.