类型“对象”不满足约束“文档”

时间:2021-01-21 16:42:19

标签: node.js mongodb typescript express mongoose

我正在尝试运行一个 MERN 应用程序来验证它所拥有的内容,但它在多个文件中向我发送了此错误。

错误:类型“CatalogType”不满足约束“文档”。 “CatalogType”类型缺少“Document”类型中的以下属性:$ignore、$isDefault、$isDeleted、$isEmpty 和 45 more.ts(2344)

enter image description here

我的代码:

const {
      Types: { ObjectId },
    } = Schema;
    
    export interface CatalogType {
      _id: any;
      code: string;
      description: string;
      version: number;
    }
    
export interface CatalogDocumentType extends Document,CatalogType{
      _id: number;
    }
    
export const schema = new Schema<CatalogType>(
      {
        id: {
          type: ObjectId,
        },
        code: {
          type: String,
          required: true,
        },
        description: {
          type: String,
          required: false,
        },
        version: {
          type: Number,
          required: false,
        },
      },
      {
        timestamps: {
          currentTime: () => new TimeZone().getLocaleCSTfromGMT(),
        },
      },
    );
    
export const Model = model<CatalogDocumentType>('Catalog', schema);

请帮帮我!

1 个答案:

答案 0 :(得分:2)

泛型类SET SERVEROUT ON; \ CREATE OR REPLACE FUNCTION CountViewers(nameofPlay plays.play%TYPE) RETURN NUMBER AS NUM NUMBER; BEGIN SELECT SUM(registerd) INTO num1 FROM plays WHERE play=nameofPlay; return num; END; DECLARE inc integer; res NUMBER; invalid_status EXCEPTION; CURSOR clients2 IS SELECT * FROM plays; name_of_play plays.play%ROWTYPE; play_name plays%TYPE; BEGIN inc:=0; name_of_play := '&Play'; OPEN clients2; LOOP FETCH clients2 INTO play_name; IF name_of_play != play_name.play and clients2%rowcount>i THEN inc:=inc+1; ELSIF name_of_play = play_name.play THEN EXIT WHEN clients2%found; ELSE RAISE invalid_status; --like throw error END IF; EXIT WHEN clients2%notfound; END LOOP; res:=CountViewers(name_of_play); DBMS_OUTPUT.PUT_LINE(name_of_play ||' | '|| res); EXCEPTION WHEN invalid_status THEN DBMS_OUTPUT.PUT_LINE('The name of play is not found'); END; 的泛型参数有泛型约束,见index.d.ts

Schema

泛型参数 class Schema<DocType extends Document = Document, M extends Model<DocType> = Model<DocType>> extends events.EventEmitter {/**..*/} 必须满足 DocType 类型约束。所以正确的做法是:

Document

有关详细信息,请参阅mongoosejs-typescript-docs

包版本:

import { Schema, model, Document, Model } from 'mongoose';

const {
  Types: { ObjectId },
} = Schema;

export interface CatalogTypeDocument extends Document {
  _id: number;
  code: string;
  description: string;
  version: number;
}

export interface CatalogTypeModel extends Model<CatalogTypeDocument> {}

export const schema = new Schema<CatalogTypeDocument>({
  id: {
    type: ObjectId,
  },
  code: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: false,
  },
  version: {
    type: Number,
    required: false,
  },
});

export const CatalogTypeModel = model<CatalogTypeDocument, CatalogTypeModel>('Catalog', schema);