Categories
JavaScript Answers

How to Create and Use Enum in Mongoose and JavaScript?

To create and use enum in Mongoose and JavaScript, we can add an enum field into the schema.

For instance, we write

const userSchema = new mongoose.Schema({
  userType: {
    type: String,
    enum: ["user", "admin"],
    default: "user",
  },
});

to create a schema with Schema.

We call it with an object that has the enum property with the possible values for the enum to create the userType enum field that is a string that can either be 'user' or 'admin'.

Categories
JavaScript Answers

How to execute an external program from within Node.js?

To execute an external program from within Node.js, we use the exec function.

For instance, we write

const exec = require("child_process").exec;
exec("pwd", (error, stdout, stderr) => {
  // result
});

to call exec with the command we want to run and the callback that’s called when the command is finished running.

We get its output from stdout.

And we get the command’s error output from stderr.

Categories
JavaScript Answers

How to POST data with request module on Node.js?

To POST data with request module on Node.js, we call the request.post method.

For instance, we write

const request = require("request");

request.post(
  {
    headers: { "content-type": "application/x-www-form-urlencoded" },
    url: "http://localhost/test2.php",
    body: "mes=heydude",
  },
  (error, response, body) => {
    console.log(body);
  }
);

to call request.post with an object that has the headers, url and the request body.

We get the response and the response body from the callback.

Categories
JavaScript Answers

How to fix the upstream dependency conflict installing NPM packages with JavaScript?

To fix the upstream dependency conflict installing NPM packages with JavaScript, we run npm install with the --legacy-peer-deps option.

For instance, we run

npm install --legacy-peer-deps

to install all packages in package.json with the packages’ peer depdendencies.

Categories
JavaScript Answers

How to use Nodemailer with Gmail and Node.js and JavaScript?

To use Nodemailer with Gmail and Node.js and JavaScript, we call the sendMail method.

For instance, we write

const nodemailer = require("nodemailer");
const smtpTransport = require("nodemailer-smtp-transport");

const transporter = nodemailer.createTransport(
  smtpTransport({
    service: "gmail",
    host: "smtp.gmail.com",
    auth: {
      user: "somerealemail@gmail.com",
      pass: "realpasswordforaboveaccount",
    },
  })
);

const mailOptions = {
  from: "somerealemail@gmail.com",
  to: "friend@gmail.com",
  subject: "Sending Email using Node.js[nodemailer]",
  text: "That was easy!",
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log(error);
  } else {
    console.log("Email sent: " + info.response);
  }
});

to create a transporter object with the createTransport method.

We call it with the object returned by smtpTransport with the username, password, and server host name to send email via SMTP.

Then we create the mailOptions object with the email options.

We call transporter.sendMail method with the mailOptions to send the message.