I've got a Mongoose on TypeScript, and it worked without interfaces and types defining, but when I decided to define types, the madness started. I've watched tons of manuals and topics and found nobody with a similar problem. I tried to define a model strictly by the manual, but I'm getting an error. Here's my model file:
import mongoose from 'mongoose';
import { nanoid } from 'nanoid';
const GeoSchema = new mongoose.Schema({
type: {
type: String,
default: 'Point',
enum: ['Point'],
},
coordinates: {
type: [Number, Number],
index: '2dsphere',
},
});
interface postInterface extends mongoose.Document {
shortId: string;
createdBy: string;
title: string;
description: string;
tags: string[];
location: {};
images: string[];
contacts: {
email: Boolean;
wa: Boolean;
phone: Boolean;
};
type: 'available' | 'wanted';
is_moderated: Boolean;
}
export const PostSchema = new mongoose.Schema(
{
shortId: {
type: String,
index: true,
default: () => nanoid(),
},
createdBy: {
type: String,
index: true,
},
title: String,
description: String,
tags: [String],
location: GeoSchema,
images: [String],
contacts: {
email: Boolean,
wa: Boolean,
phone: Boolean,
},
type: { type: String, enum: ['available', 'wanted'] },
is_moderated: Boolean,
},
{ timestamps: true }
);
export const Post = mongoose.model<postInterface>('Post', PostSchema);
When I import it into another file, on the line
return Post.create(post)
the linter gives me the error:
Type 'postInterface & { _id: any; }' is missing the following properties from type 'Schema<any, Model<any, any, any, any>, {}, {}>': add, childSchemas, clearIndexes, clone, and 37 more.ts(2740)
I googled examples of typescript model defining, and it looks like something that works for others doesn't work for me.
What am I doing wrong?
Update
Man, it was so obvious! The file where I imported the model looks like this:
import { Post, PostSchema } from '../models/post.model';
export class PostService {
async createPost(post: any): Promise<typeof PostSchema | null> {
return Post.create(post);
}
async getPostById(shortId: string): Promise<typeof PostSchema | null> {
return Post.findOne({ shortId });
}
}
So the thing was in Promise<typeof PostSchema | null>
. When I changed the code to this, the error was gone:
import { Post, postInterface } from '../models/post.model';
export class PostService {
async createPost(post: any): Promise<postInterface | null> {
return Post.create(post);
}
async getPostById(shortId: string): Promise<postInterface | null> {
return Post.findOne({ shortId });
}
}
Thanks to @lpizzinidev for pushing me in the right direction!
CodePudding user response:
Man, it was so obvious! The file where I imported the model looks like this:
import { Post, PostSchema } from '../models/post.model';
export class PostService {
async createPost(post: any): Promise<typeof PostSchema | null> {
return Post.create(post);
}
async getPostById(shortId: string): Promise<typeof PostSchema | null> {
return Post.findOne({ shortId });
}
}
So the thing was in Promise<typeof PostSchema | null>
. When I changed the code to this, the error was gone:
import { Post, postInterface } from '../models/post.model';
export class PostService {
async createPost(post: any): Promise<postInterface | null> {
return Post.create(post);
}
async getPostById(shortId: string): Promise<postInterface | null> {
return Post.findOne({ shortId });
}
}
Thanks to @lpizzinidev for pushing me in the right direction!