arashsheyda / nuxt-mongoose

A Nuxt module for simplifying the use of Mongoose in your project.
https://docs.arashsheyda.me/nuxt-mongoose
69 stars 11 forks source link

Not able to add default value #35

Open typhoon11 opened 7 months ago

typhoon11 commented 7 months ago

When trying to add default value it's giving typescript error. image

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
    }
})
Timothy1102 commented 7 months ago

having the same issue

harjot-stack commented 5 months ago

having same issue

oukhrib commented 5 months ago

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: Screenshot 2024-01-13 at 01 14 05To:

To

Screenshot 2024-01-13 at 01 15 02

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

benjamindedonder commented 4 months ago

Hey all, I had the same issue but it went away when I provided a type to the defineMongooseModel function, like so:

Screenshot from 2024-02-27 12-21-39

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)

Screenshot from 2024-02-27 12-29-27

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.