Typegoose: Arrays without the option "type" or "items" makes array "Mixed" (adding array in array)

Created on 23 Jun 2020  路  8Comments  路  Source: typegoose/typegoose

Versions

  • NodeJS: 12.0.0
  • Typegoose(NPM): 7.x.x
  • Typegoose(GIT): commithash
  • mongoose: 5.9.20
  • mongodb: 3.5.9

What is the Problem?

Arrays prior to 7.1.0 are stored correctly. In versions later than 7.1 each element is nested in an own array:

Code Example

class Example {
  @prop()
  public arrayExample!: string[]
}

const arrayExample: ["1","2","3"]

new Example({arrayExample}).save()

My expectations in DB are:

{
   _id: ...,
   arrayExample: ["1","2","3"]
}

Its instead:

{
   _id: ...,
   arrayExample: [["1"],["2"],["3"]]
}

Do you know why it happenes?

no

question

Most helpful comment

class Something {
  @prop({ items: String })
  public languages?: string[];
}

Note the items:String.

Scott

All 8 comments

class Something {
  @prop({ items: String })
  public languages?: string[];
}

Note the items:String.

Scott

as @smolinari already said, items: the.actual.type (or now type: the.actual.type) is still needed, it was already needed prior to 7.1

Edit: for an more in-depth explanation on why: https://github.com/microsoft/TypeScript/issues/7169

updated title to better reflect what this issue is (because of duplicates)

The "items" property is now deprecated what would be the correct way to fix this now? When I set type to [String] I get Error: "Example.arrayExample"'s Type is invalid! Type is: "function String() { [native code] }" [E009]. I am only able to fix this by setting type to Schema.Types.Mixed.

@henhen724 please give an example on how you try to set it
(btw, did you try to do type: [String]? when yes, this is only available since 7.4.0)

Yes I did use type: [String], but I am using 7.3.5 so it should work. Here is a repo with a minimal reproducible example: https://github.com/henhen724/typegoose-issue-example. Both the dev and start will throw the error I'm talking about.

like i said in my earlier comment

did you try to do type: [String]? when yes, this is only available since 7.4.0

this syntax is only available since 7.4.0

Sorry, I missed read that

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gserracalahorra picture gserracalahorra  路  6Comments

DanielEberl picture DanielEberl  路  5Comments

dandv picture dandv  路  10Comments

huming2207 picture huming2207  路  8Comments

hmgibson23 picture hmgibson23  路  10Comments