Categories
JavaScript Answers

How to fix ECONNREFUSED error when connecting to MongoDB from Node.js?

To fix ECONNREFUSED error when connecting to MongoDB from Node.js, we call connect.

For instance, we write

const mongoose = require("mongoose");

const mongoURI = "mongodb://localhost:27017/test";
const MongoDB = mongoose.connect(mongoURI).connection;
MongoDB.on("error", (err) => {
  console.log(err.message);
});
MongoDB.once("open", () => {
  console.log("mongodb connection open");
});

to call connect to connect to MongoDB with the mongoURI.

We get the connection with the connection property.

And we call on to catch errors.

We call once to catch the first emission of the open event.

Categories
JavaScript Answers

How to fix install Node npm package fails with 404?

To fix install Node npm package fails with 404, we set the npm registry.

For instance, we run

npm config set registry http://registry.npmjs.org

to run npm config set registry to set the package registry URL to the default npm registry URL.

Categories
JavaScript Answers

How to fix ‘Access-Control-Allow-Origin’ issue when API call made from React isomorphic app?

To fix ‘Access-Control-Allow-Origin’ issue when API call made from React isomorphic app, we enable CORS on the back end.

For instance, we write

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});

to enable CORS in a middleware by sending the Access-Control-Allow-Origin and Access-Control-Allow-Headers response headers.

Categories
JavaScript Answers

How to get Express.js to 404 only on missing routes?

To get Node Express.js to 404 only on missing routes, we can return a 404 response with a middleware.

For instance, we write

router.use((req, res, next) => {
  next({
    status: 404,
    message: "Not Found",
  });
});

router.use((err, req, res, next) => {
  if (err.status === 404) {
    return res.status(400).render("404");
  }

  if (err.status === 500) {
    return res.status(500).render("500");
  }

  next();
});

at the bottom of our code to call next to send the 404 status to the last middleware.

Then we check the err.status for the value and call res.status to return the status we want.

And we call render to render the response body.

Categories
JavaScript Answers

How to add nested objects in Node Mongoose schemas?

To add nested objects in Node Mongoose schemas, we set the type property to another schema.

For instance, we write

const AddressSchema = mongoose.Schema({
  city: String,
  street: String,
  houseNumber: String,
});

const ContactInfoSchema = mongoose.Schema({
  tel: [Number],
  email: [String],
  address: {
    type: AddressSchema,
    required: true,
  },
});

const CustomerSchema = mongoose.Schema({
  firstName: String,
  lastName: String,
  company: String,
  connectInfo: ContactInfoSchema,
});

to create the CustomerSchema that has the connectInfo property set to the ContactInfoSchema.

And the ContactInfoSchema has the address property set to the AddressSchema as its type.