Categories
JavaScript Answers

How to fix Error: Failed to lookup view in Node Express?

To fix Error: Failed to lookup view in Node Express, we should use a valid path.

For instance, we write

const path = require("path");
app.use(express.static(path.join(__dirname + "../public")));

to call express.static foplder with the path returned by path.join to get the path of the /public folder one level above __dirname to expose the folder as the static files folder.

Categories
JavaScript Answers

How to fix webpack command not working with JavaScript?

To fix webpack command not working with JavaScript, we add the webpack command to a script.

For instance, we write

{
  "scripts": {
    "build": "webpack --config webpack.config.js"
  }
}

in package.json to add the build script.

We set it to run the webpack command with the webpack.config.js config.

Categories
JavaScript Answers

How to set ObjectId as a data type in Node Mongoose?

To set ObjectId as a data type in Node Mongoose, we set the value to ObjectId.

For instance, we write

const mongoose = require("mongoose");

const Schema = mongoose.Schema,
  ObjectId = Schema.ObjectId;

const SchemaProduct = new Schema({
  categoryId: ObjectId,
  title: String,
  price: Number,
});

to set the categoryId property to ObjectId to make categoryId an object ID field.

Categories
JavaScript Answers

How to do multiple populates with Node Mongoose?

To do multiple populates with Node Mongoose, we call populate multiple times.

For instance, we write

OrderModel.find()
  .populate("user")
  .populate("meal")
  .exec((err, results) => {
    // callback
  });

to call populate to populate the user and meal field entries in the result.

We get the results from results in the callback.

Categories
JavaScript Answers

How to specify specific fields with Node Sequelize instead of *?

To specify specific fields with Node Sequelize instead of *, we set the attributes property.

For instance, we write

const list = await template.findAll({
  where: {
    user_id: req.params.userId,
  },
  attributes: ["id", "template_name"],
});
res.status(200).json(list);

to call findAll with an object with the attributes property set to an array of columns to select.