3

Typescript error

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.

2
  • I'm having the same error. Let me know if you find a solution please! Commented Dec 3, 2020 at 16:28
  • Its weird, I got rid of this error after I removed one of the route I recently created. Its not an issue with the schema. Commented Dec 3, 2020 at 18:50

2 Answers 2

1

according to mongoose documentation some changes are needed.

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 };
Sign up to request clarification or add additional context in comments.

Comments

1

The solution I found was to remove @types/mongoose and update mongoose to its latest version. It seems like latest mongoose version already ships with its types included and it's causing a conflict.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.