arraystypescriptmongoosetuplestypegoose

Typegoose + mongoose: how to push tuple to array?


I'm working on the following code:

import { prop, modelOptions, DocumentType, getModelForClass } from "@typegoose/typegoose";
import dbConnect from "src/lib/dbConnect";
import mongoose from 'mongoose';


@modelOptions({ schemaOptions: { timestamps: true } })
class Kitten {
  @prop()
  public name?: string;

  @prop({ type: String, required: true, default: [] })
  public events!: mongoose.Types.Array<[string, Date]>;
  // also doesn't work:
  // public events!: [string, Date][];


  // instance method:
  public async addEvent(this: DocumentType<Kitten>, _eventName: string) {

    const tuple : [string, Date] = [_eventName, new Date()];

    this.events.push(tuple);
    await dbConnect();
    await this.save();
  }
}

export const KittenModel = getModelForClass(Kitten);

and when I call addEvent I get the following error when executing this.events.push(tuple); :

error - CastError: Cast to string failed for value "[
  '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001',
  2023-01-11T05:02:21.462Z
]" (type Array)

It seems that the method push is trying to convert the tuple into a string for some reason...

My environment is:

"@typegoose/typegoose": "^10.0.0",

"mongoose": "^6.8.3",

nodejs --version :v16.18.1


Solution

  • Mongoose (and typegoose by extension) dont support tuple arrays unless the field is Mixed (which is not recommended to be used).

    In your case a Map would probably what you search for, if the event names are unique, if they are not unique you may wanna use a nested class to keep the fields explicitly, like:

    class EventDate {
      @prop()
      public eventName: string;
    
      @prop()
      public date: Date;
    }
    
    class Kitten {
      @prop({ type: EventDate, required: true, default: [] })
      public events!: mongoose.Types.Array<EventDate>;
    
      public async addEvent(this: DocumentType<Kitten>, _eventName: string) {
        this.events.push({ eventName: _eventName, date: new Date() });
        await dbConnect();
        await this.save();
      }
    }