I'm trying to create an array of object but I can not do it anyone can help me please how I can declare it in the schema?
I want my array to be like this
favorite=[ {name:"shoes", rating:0} ];
I would be happy to help:)
this is my user schema
import { Document, Schema, model } from "mongoose";
export interface IUserModel extends Document {
favorite:Array<any>; //Maybe I should change that too
}
const UserSchema = new Schema<IUserModel>({
favorite:{
type: Array,
items:{
type: String
} // here is the problem how to declare array of object?
});
export const UserModel = model<IUserModel>("UserModel", UserSchema, "Users");
You can do it like this. first, you can create a model class with the name Item
export class Item {
name: null;
rating: 0;
}
Now you can use it like this as an Array.
favorite = [new Item()];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.