Home > Net >  using ObjectId as a data type
using ObjectId as a data type

Time:06-09

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

CodePudding user response:

Replace ObjectId with

mongoose.ObjectId

Or

mongoose.Types.ObjectId

Reference

Mongoose - ObjectIds

  • Related