简体   繁体   English

Nodejs - 获取 mongoose typescript 错误

[英]Nodejs - Getting mongoose typescript error

Typescript error Typescript 错误

I am using the same schema approach in all over my backend but only this one gives an error for some reason .我在整个后端都使用相同的模式方法,但由于某种原因,只有这个方法会出错 It was running fine until this morning.直到今天早上它运行良好。

All the other schemas in the website have exactly the same approach but this one is throwing an error.网站中的所有其他模式都具有完全相同的方法,但这个会引发错误。

Error:错误:

[ERROR] 19:52:58 ⨯ Unable to compile TypeScript:
src/models/project.ts(81,3): error TS2554: Expected 0-1 arguments, but got 2.
src/models/project.ts(83,17): error TS7006: Parameter 'doc' implicitly has an 'any' type.
src/models/project.ts(83,22): error TS7006: Parameter 'ret' implicitly has an 'any' type.
src/models/project.ts(90,15): error TS2551: Property 'statics' does not exist on type 'Schema'. 
Did you mean 'static'?

Schema:架构:

interface ProjectAttrs {
  user: string;
  profile: string;
  title: string;
  image: string;
  description: string;
  type: categories;
}

export interface ProjectDoc extends mongoose.Document {
  user: string;
  profile: string;
  title: string;
  image: string;
  collabImages: string[];
  description: string;
  comments: string[];
  likes: number;
  likers: ProfileDoc[];
  state: boolean;
  // requesters: ProfileDoc[];
  requests: string[];
  team: string[];
  type: categories;
}

interface ProjectModel extends mongoose.Model<ProjectDoc> {
  build(attrs: ProjectAttrs): ProjectDoc;
}

const projectSchema = new mongoose.Schema(
  {
    user: { type: String, required: true },
    profile: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "Profile",
    },
    title: { type: String, required: true },
    image: { type: String },
    collabImages: [{ type: String,
        required: false}],
    description: { type: String, required: true },
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "Comment"}],
    state: { type: Boolean, default: true },
    likes: { type: Number, default: 0 },
    likers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Profile"}],
    requests: [{ type: mongoose.Schema.Types.ObjectId, ref: "Request"}],
    team: [{ type: mongoose.Schema.Types.ObjectId, ref: "Profile"}],
    type: { type: String, required: true },
    createdAt: { type: Date, default: Date.now() },
  },
  {
    toJSON: {
      transform(doc, ret) { // ERROR 
        ret.id = ret._id;   // ERROR 
        delete ret._id;     // ERROR
      },
    },
  }
);
projectSchema.statics.build = (attrs: ProjectAttrs) => {
  return new Project(attrs);
};
const Project = mongoose.model<ProjectDoc, ProjectModel>(
  "Project",
  projectSchema
);
export { Project };

I wonder what is it that I am missing here.我想知道我在这里错过了什么。 I would really appreciate the help.我非常感谢您的帮助。

The solution I found was to remove @types/mongoose and update mongoose to it latest version.我找到的解决方案是删除 @types/mongoose 并将 mongoose 更新到最新版本。 Seems like latest mongoose version already ships with its types included and its causing a conflict似乎最新的 mongoose 版本已经包含其类型并导致冲突

according to mongoose documentation some changes are needed.根据 mongoose 文档,需要进行一些更改。

please look following code请看下面的代码

import mongoose from 'mongoose';

interface ProjectAttrs {
  user: string;
  profile: string;
  title: string;
  image: string;
  description: string;
  type: categories;
}

export interface ProjectDoc extends mongoose.Document {
  user: string;
  profile: string;
  title: string;
  image: string;
  collabImages: string[];
  description: string;
  comments: string[];
  likes: number;
  likers: ProfileDoc[];
  state: boolean;
  // requesters: ProfileDoc[];
  requests: string[];
  team: string[];
  type: categories;
}

interface ProjectModel extends mongoose.Model<ProjectDoc> {
  build(attrs: ProjectAttrs): ProjectDoc;
}

const projectSchema = new mongoose.Schema(
  {
    user: { type: String, required: true },
    profile: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "Profile",
    },
    title: { type: String, required: true },
    image: { type: String },
    collabImages: [{ type: String,
        required: false}],
    description: { type: String, required: true },
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "Comment"}],
    state: { type: Boolean, default: true },
    likes: { type: Number, default: 0 },
    likers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Profile"}],
    requests: [{ type: mongoose.Schema.Types.ObjectId, ref: "Request"}],
    team: [{ type: mongoose.Schema.Types.ObjectId, ref: "Profile"}],
    type: { type: String, required: true },
    createdAt: { type: Date, default: Date.now() },
  }
);



//REVIEW THIS PART SINCE SOMETHING MIGHT BE MISSING
schema.set('toJson', {transform:function(doc: any, ret:any) {
  ret.id = ret._id;
  delete ret._id;
}});


//THIS WAS CHANGED NO LONGER STATICS
projectSchema.static('build', (attrs: ProjectAttrs) => {
  return new Project(attrs);
});


const Project = mongoose.model<ProjectDoc, ProjectModel>(
  "Project",
  projectSchema
);
export { Project };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM