Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

node.js - Mongoose + Typescript -> Exporting model interface error

I am currently new to MERN and trying to run an application but I have a number of repeating errors in my project starting with this: Property '_id' is optional in type 'CatalogDocumentType' but required in type 'CatalogType', most of my models have these properties, I have no idea what is going on.

this error: Interface 'CatalogDocumentType' cannot simultaneously extend types 'CatalogType' and 'Document'. Named property '_id' of types 'CatalogType' and 'Document' are not identical.

private async addCatalog(req: Request, res: Response) {
    try {
      const { code, description, version } = req.body;
      const catalog = await this.catalog.findOne({ code }).exec();

      if (catalog) return BadRequest(res, 'Catalog code already exists');

      const catalogsCatalog = new CatalogModel({
        code,
        description,
        version,
      });
      const catalogsAdded = await catalogsCatalog.save();

      return Ok<CatalogType>(res, catalogsAdded); <-- catalogsAdded sintax error here
    } catch (error) {
      console.log('error', error);
      return ServerError(res);
    }
  }

export interface CatalogType {
      _id: string;
      code: string;
      description: string;
      version: number;
    }

export const Model = model<CatalogDocumentType>('Catalog', schema);

Errors:
[![enter image description here][1]][1]
  [1]: https://i.stack.imgur.com/sonPw.png

any idea?

question from:https://stackoverflow.com/questions/65830172/mongoose-typescript-exporting-model-interface-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...