using ObjectId as a data type

Issue

I’m trying to create a schema for an object and I’m using ObjectId as type but he gives me an error

const mongoose = require("mongoose");

const permissionsSchema = new mongoose.Schema({
  roleId: {
    type: ObjectId,
    required: true,
  },
  menuPageId: {
    type: ObjectId,
    required: true,
  },
  readAccess: {
    type: Boolean,
    required: true,
  },
  createAccess: {
    type: Boolean,
    required: true,
  },
  editAccess: {
    type: Boolean,
    required: false,
  },
  deleteAccess: {
    type: Boolean,
    required: false,
  },
});

const Permissions = mongoose.model("Permissions", permissionsSchema);
module.exports = Permissions;

ERROR

type: ObjectId,
ReferenceError: ObjectId is not defined

Solution

Replace ObjectId with

mongoose.ObjectId

Or

mongoose.Types.ObjectId

Reference

Mongoose – ObjectIds

Answered By – Yong Shun

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.