Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to add default value #35

Open
typhoon11 opened this issue Nov 28, 2023 · 4 comments
Open

Not able to add default value #35

typhoon11 opened this issue Nov 28, 2023 · 4 comments

Comments

@typhoon11
Copy link

typhoon11 commented Nov 28, 2023

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
Copy link

having the same issue

@harjot-stack
Copy link

having same issue

@oukhrib
Copy link

oukhrib commented Jan 13, 2024

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants