The Menu Schema
const mongoose = require("mongoose");
const schema = new mongoose.Schema({
name: String,
isDropDown: Boolean,
dropItems: {
type: [mongoose.Schema.Types.ObjectId],
refPath: "dropItemModel",
},
dropItemModel: {
type: mongoose.Schema.Types.ObjectId,
enum: ["Category", "Archive", "Author"],
},
});
module.exports = mongoose.models.Menu || mongoose.model("Menu", schema);
/**
* what I understand from this so far
* Define the field object type and use a refPath instead of ref
*
* Define the refPath object
*
* When creating a document passing your desired value
*
* Populate
*/
###The Menu Controller####
import connectdb from "../db/connect";
import menuModel from "../models/menu.model";
export default async function menuController() {
connectdb();
const menus = await menuModel
.find({}, { _v: 0 })
.populate("dropItems" );
return JSON.stringify(menus);
};
What am I doing wrong. I gone to the moon and back the problem still persists.
“mongoose”: “^6.10.0”,
Mongoose Doc: https://mongoosejs.com/docs/populate.html#dynamic-ref
I have tried everything on refPaths I could find and they basically saying the same thing mongoose docs is saying. So I don’t know what to do about the situation anymore. Refs work as expected but refPaths is the issue here.
NOTE: I’m using NextJS