Fayeaz Ahmed
Fayeaz Ahmed

Reputation: 127

TypeError: Invalid schema configuration: `True` is not a valid type at path `timestamps`?

At first, let me assure you I've searched everywhere I can about this error, but it seemed like nothing close to this sort exists. I used timestamps: true, in my mongoose scheme. And that should generate "created_at" and "updated_at" automatically in my schema. But this line throws this error: TypeError: Invalid schema configuration: 'True' is not a valid type at path 'timestamps'. Then I tried writing timestamps: {Type:Date, required:true}. In that case there is no runtime error, but when I post a request, it throws basically a validation error as timestamps is required but not provided in the body. And that obviously means the keyword timestamps is not working as it should be, cause it's supposed to be generated automatically.

Trust me, when I say that I tried to find the problem on my own as much as I can, because the thought of posting a new question here kinda gives me nightmare :3 love stack overflow though <3

Upvotes: 6

Views: 10940

Answers (2)

skull
skull

Reputation: 161

Don't pass it in the schema itself, pass {timestamps: true} as the options in constructor.

const exampleSchema = new Schema(
    {
        name: {
            type: String,
        },
    },
    {
        timestamps: true,
    }
);

Upvotes: 10

Abdulsalam Lukmon
Abdulsalam Lukmon

Reputation: 21

For mongoose 5.x timestamp is automatically created with the mongodb cluster you create. Delete timstamps, and use 'date' if you really need timestamps or you can use

$ npm install --save mongoose mongoose-timestamp 

const mongoose = require('mongoose'), timestamps = require('mongoose-timestamp')

Upvotes: 0

Related Questions