【问题标题】:Nodejs - Getting mongoose typescript errorNodejs - 得到猫鼬打字稿错误
【发布时间】:2021-03-13 21:06:23
【问题描述】:

打字稿错误

我在整个后端都使用相同的架构方法,但由于某种原因,只有这一种会出错。直到今天早上它都运行良好。

网站中的所有其他模式都有完全相同的方法,但这个会引发错误。

错误:

[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'?

架构:

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 };

我想知道我在这里缺少什么。非常感谢您的帮助。

【问题讨论】:

  • 我遇到了同样的错误。如果您找到解决方案,请告诉我!
  • 这很奇怪,在我删除了我最近创建的一条路线后,我摆脱了这个错误。这不是架构的问题。

标签: javascript node.js mongodb typescript mongoose


【解决方案1】:

根据 mongoose 文档,需要进行一些更改。

请看下面的代码

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 };

【讨论】:

    【解决方案2】:

    我找到的解决方案是删除 @types/mongoose 并将 mongoose 更新到最新版本。似乎最新的 mongoose 版本已经包含了它的类型并导致了冲突

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 2016-06-14
      • 2020-06-21
      • 2020-05-11
      • 2016-04-01
      • 1970-01-01
      • 2017-04-11
      • 2017-01-20
      • 2021-01-05
      相关资源
      最近更新 更多