nuxt-mongoose
nuxt-mongoose copied to clipboard
Not able to add default value
When trying to add default value it's giving typescript error.
import { defineMongooseModel } from '#nuxt/mongoose'
export const UserSchema = defineMongooseModel({
name: 'User',
schema: {
id: { type: String, required: true },
accessToken: { type: String, required: true },
refreshToken: { type: String, required: true },
email: { type: String, required: true },
amizone: {
username: { type: String, required: true },
password: { type: String, required: true },
},
encryptionKey: { type: String, required: true },
notification: { type: Boolean, default: false }
},
options: {
timestamps: true
}
})
having the same issue
having same issue
Hi there, I came across your issue and thought I might offer a suggestion. You could try adding as any after the field in question. It looks like this might help bypass the TypeScript error you're encountering.
For example, change:
To:
To
Just a heads up, using as any is a quick fix and might not be the best long-term solution, but it could help you move forward for now.
Hope this helps!
@typhoon11 @Timothy1102 @harjot-stack
Hey all, I had the same issue but it went away when I provided a type to the defineMongooseModel
function, like so:
Type '{ type: DateConstructor; default: Date; }' is not assignable to type 'SchemaDefinitionProperty<DateConstructor, any> | undefined'.
Type '{ type: DateConstructor; default: Date; }' is not assignable to type 'undefined'.ts(2322)
Type '{ type: StringConstructor; required: true; validate: { validator(v: string): boolean; }; }' is not assignable to type 'SchemaDefinitionProperty<StringConstructor, any> | undefined'.
Types of property 'validate' are incompatible.
Type '{ validator(v: string): boolean; }' is not assignable to type 'Function | RegExp | [RegExp, string] | [Function, string] | ValidateOpts<Mixed> | ValidateOpts<Mixed>[] | ... 6 more ... | undefined'.
Types of property 'validator' are incompatible.
Type '(v: string) => boolean' is not assignable to type 'ValidateFn<Mixed> | LegacyAsyncValidateFn<Mixed> | AsyncValidateFn<Mixed> | ValidateFn<StringConstructor> | LegacyAsyncValidateFn<...> | AsyncValidateFn<...> | undefined'.
Type '(v: string) => boolean' is not assignable to type 'ValidateFn<Mixed>'.
Types of parameters 'v' and 'value' are incompatible.
Type 'Mixed' is not assignable to type 'string'.ts(2322)
export interface Post {
slug: string
created?: Date
language: 'nl' | 'en'
tags: Array<string>
title: string
content: string
}
I don't really understand how it works as I'm not very proficient with TypeScript, but I thought I'd share it with you.