nuxt-mongoose icon indicating copy to clipboard operation
nuxt-mongoose copied to clipboard

Not able to add default value

Open typhoon11 opened this issue 1 year ago • 4 comments

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
    }
})

typhoon11 avatar Nov 28 '23 09:11 typhoon11

having the same issue

Timothy1102 avatar Dec 02 '23 05:12 Timothy1102

having same issue

harjot-stack avatar Jan 10 '24 09:01 harjot-stack

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

oukhrib avatar Jan 13 '24 00:01 oukhrib

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.

benjamindedonder avatar Feb 27 '24 11:02 benjamindedonder