Search code examples
typescriptmongodbmongoosemongoose-schema

Typescript error : Expected 0-1 arguments, but got 2


I have mongoose schema which uses timestamp. I tried to run the code it errors out with

error TS2554: Expected 0-1 arguments, but got 2.

  { timestamps: true }

the following is the code schema

const Schema = mongoose.Schema;
const loginUserSchema = new Schema(
 {
    userId: { type: String, required: false }
 },
  { timestamps: true }
);

loginUserSchema.index({ userName: 'text' });

export const LoginUserModel = mongoose.model("loginUser", loginUserSchema) ;

my package.json

  "dependencies": {
    "@types/express": "^4.17.9",
    "@types/mongoose": "^5.7.7",
    "@types/node": "^13.9.2",
    "@types/react-router-dom": "^5.1.3",
    "body-parser": "^1.13.3",
    "config": "^3.3.2",
    "cors": "^2.8.1",
    "express": "^4.13.3",
    "express-session": "^1.14.2",
    "jsonwebtoken": "^8.5.1",
    "keycloak-connect": "github:keycloak/keycloak-nodejs-connect",
    "moment": "^2.29.1",
    "mongodb": "^3.5.5",
    "mongoose": "^5.10.9",
    "nodemon": "^2.0.5",
    "react-router-dom": "^5.1.2",
    "socket.io": "^2.3.0",
    "ts-node": "^8.10.2",
    "typescript": "^3.9.7"
  }

Any changes I need to make this work?


Solution

  • This may be an error in @types/mongoose You should be able to use:

    loginUserSchema.set('timestamps', true)
    

    To get around it.