admin管理员组文章数量:1122846
I am using mongoose with Nextjs. If you look at the Review Schema below. It is linked with User schema and I am using the Review schema with populate in my React page to get the User document data, i-e; fullName.
Is there a way i can get types around the User document when populated inside Review?
I've tried (review?.user as User)?.fullName) which I've attached as well.
import { Document, InferSchemaType, Schema, model, models } from 'mongoose';
export const ReviewSchema = new Schema(
{
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
business: { type: Schema.Types.ObjectId, ref: 'Business', required: true },
rating: { type: Number, required: true, min: 1, max: 5 },
comment: { type: String, required: false },
images: { type: [String], required: false },
},
{
timestamps: true,
},
);
export type Review = InferSchemaType<typeof ReviewSchema> & Document;
export const ReviewModel = models.Review || model('Review', ReviewSchema);
import { Document, InferSchemaType, Schema, model, models } from 'mongoose';
const UserSchema = new Schema({
email: {
type: String,
unique: [true, 'Email already exists!'],
required: [true, 'Email is required!'],
},
fullName: {
type: String,
required: [true, 'Fullname is required!'],
},
role: {
type: String,
enum: ['user', 'admin', 'owner'],
default: 'user',
},
password: { type: String },
image: { type: String },
businesses: [{ type: Schema.Types.ObjectId, ref: 'Business' }],
});
export type User = InferSchemaType<typeof UserSchema> & Document;
export const UserModel = models.User || model('User', UserSchema);
I am using mongoose with Nextjs. If you look at the Review Schema below. It is linked with User schema and I am using the Review schema with populate in my React page to get the User document data, i-e; fullName.
Is there a way i can get types around the User document when populated inside Review?
I've tried (review?.user as User)?.fullName) which I've attached as well.
import { Document, InferSchemaType, Schema, model, models } from 'mongoose';
export const ReviewSchema = new Schema(
{
user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
business: { type: Schema.Types.ObjectId, ref: 'Business', required: true },
rating: { type: Number, required: true, min: 1, max: 5 },
comment: { type: String, required: false },
images: { type: [String], required: false },
},
{
timestamps: true,
},
);
export type Review = InferSchemaType<typeof ReviewSchema> & Document;
export const ReviewModel = models.Review || model('Review', ReviewSchema);
import { Document, InferSchemaType, Schema, model, models } from 'mongoose';
const UserSchema = new Schema({
email: {
type: String,
unique: [true, 'Email already exists!'],
required: [true, 'Email is required!'],
},
fullName: {
type: String,
required: [true, 'Fullname is required!'],
},
role: {
type: String,
enum: ['user', 'admin', 'owner'],
default: 'user',
},
password: { type: String },
image: { type: String },
businesses: [{ type: Schema.Types.ObjectId, ref: 'Business' }],
});
export type User = InferSchemaType<typeof UserSchema> & Document;
export const UserModel = models.User || model('User', UserSchema);
Share
Improve this question
asked Nov 22, 2024 at 8:06
kazmi066kazmi066
6256 silver badges18 bronze badges
1 Answer
Reset to default 0user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
change this field -> userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }
, and add new field user: { type: mongoose.Schema.Types.Mixed}
and when you populate user for reviews, set {as: "user"}
and then you can access user as entity.
it is not best solution for your case...
本文标签: reactjsType autocompletion for populated mongoose documentStack Overflow
版权声明:本文标题:reactjs - Type autocompletion for populated mongoose document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305251a1932522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论