0

I'm trying to print the name of a class that is being used to create a new object, but running into an issue because the class is a mongoose model that was created using mongoose.model():

const mongoose = require("mongoose");

const genreSchema = mongoose.Schema({ name: String });
const Genre = mongoose.model("Genre", genreSchema);

async function create(Model, modelInfo) {
  const instance = new Model(modelInfo);
  // Await some asynchronous work
  console.log(instance.constructor.name, "created: ", instance);
}

create(Genre, { name: "Thriller" });

When doing console.log(instance.constructor.name), the result is model. I was hoping for Genre, but I'm guessing this happens because constructor in this case is referring to the constructor for the Genre class (prototype chaining I believe), which is model (mongoose.model()).

If I try console.log(Genre.constructor.name) the result is function (which I am guessing refers to the constructor of the mongoose.model()), and console.log(Genre.name) yields model as well. console.log(instance.name) of course yields Thriller.

Anybody know how I can get it to print out Genre?

eric_b_231
  • 15
  • 4

0 Answers0