Currently, I am focused on managing products and categories
These are the two types I have created:
type Category = {
parent: Category | null; // Is this acceptable?
name: String;
};
type Product = {
categories: Category[];
name: String;
qty: Number;
price: Number;
};
Next, I proceeded to create the models:
// Category model
import { Schema, Types, model } from "mongoose";
import Category from "../types/Category";
const categorySchema = new Schema(
{
parent: { type: Types.Array<Category>, required: true },
name: { type: String, required: true },
},
{ timestamps: true }
);
export default model("Product", categorySchema);
// Product model
import { Schema, Types, model } from "mongoose";
import Category from "../types/Category";
const productSchema = new Schema(
{
categories: { type: Types.Array<Category>, required: true },
name: String,
qty: Number,
price: Number,
},
{ timestamps: true }
);
export default model("Product", productSchema);
Although VS Code does not display any errors, the server presents the following error upon execution:
TypeError: Invalid schema configuration: MongooseArray
is not a valid type at path categories
.
Isn't the type of categories in productSchema supposed to be an array of Category?
I also attempted
categories: { type: [Category], required: true }
, but it did not work.