Categories
JavaScript Answers

How to stop Mongoose from creating _id property for sub-document array items?

Sometimes, we want to stop Mongoose from creating _id property for sub-document array items.

In this article, we’ll look at how to stop Mongoose from creating _id property for sub-document array items.

How to stop Mongoose from creating _id property for sub-document array items?

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.

For instance, we write

const mongoose = require("mongoose");

const subSchema = mongoose.Schema({}, {
  _id: false
});

const schema = mongoose.Schema({
  subSchemaCollection: [subSchema]
});

const model = mongoose.model('tablename', schema);

to create subSchema by calling mongoose.Schema with an object that has _id set to false.

Then we put the subSchema in the parent schema in the array we set as the value of subSchemaCollection.

Conclusion

To stop Mongoose from creating _id property for sub-document array items, we can call mongoose.Schema with _id set to false.

Categories
JavaScript Answers

How to call a Python function from Node.js?

Sometimes, we want to call a Python function from Node.js.

In this article, we’ll look at how to call a Python function from Node.js.

How to call a Python function from Node.js?

To call a Python function from Node.js, we can call the spawn method.

For instance, we write

const {
  spawn
} = require("child_process");
const pythonProcess = spawn('python', ["path/to/script.py", arg1, arg2]);

to call the spawn method available in the child_process module.

We run python with the path to the script, arg1 and arg2 as the command line arguments.

The function we want to call should be in script.py.

Conclusion

To call a Python function from Node.js, we can call the spawn method.

Categories
JavaScript Answers

How to make a Node.js application run permanently?

Sometimes, we want to make a Node.js application run permanently.

In this article, we’ll look at how to make a Node.js application run permanently.

How to make a Node.js application run permanently?

To make a Node.js application run permanently, we can use the forever package.

We install it globally by running

sudo npm install -g forever

Then we can start our app with it with

forever server.js

We can also run it as a service with

forever start server.js

We can restricts restarts to 5 times when it crashes or stop unexpectedly with

forever -m5 server.js

We can stop a process with

forever stop 0

where 0 is the process ID.

And we can restart a process with

forever restart 0

where 0 is the process ID.

Conclusion

To make a Node.js application run permanently, we can use the forever package.

Categories
JavaScript Answers

How to send emails in Node.js?

Sometimes, we want to send emails in Node.js.

In this article, we’ll look at how to send emails in Node.js.

How to send emails in Node.js?

To send emails in Node.js, we can use the nodemailer package.

To install it, we run

npm i nodemailer

Then we use it by writing

const mailer = require("nodemailer");
const smtpTransport = mailer.createTransport("SMTP", {
  service: "Gmail",
  auth: {
    user: "abc@gmail.com",
    pass: "password"
  }
});

const mail = {
  from: "from <from@gmail.com>",
  to: "to@gmail.com",
  subject: "Send Email Using Node.js",
  text: "Node.js",
  html: "<b>Node.js</b>"
}

smtpTransport.sendMail(mail, (error, response) => {
  if (error) {
    console.log(error);
  } else {
    console.log(response.message);
  }
  smtpTransport.close();
});

we call mailer.createTransport to create the smtpTransport object that we use to log into our SMTP server.

Then we call smtpTransform.sendMail to send our email with the data coming from the mail object.

If there’s an error, then error is set in the callback.

Otherwise, response is set.

Finally, we log out of the SMTP server with smtpTransport.close.

Conclusion

To send emails in Node.js, we can use the nodemailer package.

Categories
JavaScript Answers

How to share constants in Node.js modules?

Sometimes, we want to share constants in Node.js modules.

In this article, we’ll look at how to share constants in Node.js modules.

How to share constants in Node.js modules?

To share constants in Node.js modules, we can put them all in a module and export them.

For instance, we write

./constants.js

module.exports = Object.freeze({
  MY_CONSTANT: 'some value',
  ANOTHER_CONSTANT: 'another value'
});

to set module.exports to a frozen object with some properties inside with the constant values.

We freeze the object with Object.freeze so it can’t be modified accidentally.

Then we can import the constants with

./app.js

const constants = require('./constants');

console.log(constants.MY_CONSTANT);
console.log(constants.ANOTHER_CONSTANT);

We call require with the path to ./constants.js.

Then we get the values from constants.

Conclusion

To share constants in Node.js modules, we can put them all in a module and export them.