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.

Categories
JavaScript Answers

How to share variables between files in Node.js?

Sometimes, we want to share variables between files in Node.js.

In this article, we’ll look at how to share variables between files in Node.js.

How to share variables between files in Node.js?

To share variables between files in Node.js, we can put our shared variables in a module.

For instance, we write

module.js

const name = "foobar";

exports.name = name;

to export the name variable in module.js by assigning it to exports.name.

And then in main.js, we write

main.js

const myModule = require('./module');

const name = myModule.name;

to call require to include the module.js module and assign the exported contents to myModule.

And then we get the exports.name value with myModule.name,

Conclusion

To share variables between files in Node.js, we can put our shared variables in a module.

Categories
JavaScript Answers

How to read a text file using Node.js?

Sometimes, we want to read a text file using Node.js.

In this article, we’ll look at how to read a text file using Node.js.

How to read a text file using Node.js?

To read a text file using Node.js, we call fs.readFile.

For instance, we write

const fs = require('fs')

const filename = '/foo/bar.txt'

fs.readFile(filename, 'utf8', (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data)
})

to call fs.readFile with the filename, the encoding of the text file, and a callback that has the read file data stored in the `data parameter.

If there’s an error when reading the file, err is set.

Conclusion

To read a text file using Node.js, we call fs.readFile.