Home > Software engineering >  Typegoose mongoose: how to push tuple to array?
Typegoose mongoose: how to push tuple to array?

Time:01-14

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

CodePudding user response:

The issue is that the decorator @prop was not being well defined.

It should be:

  @prop({ type: mongoose.Types.Array<[string, Date]>, required: true, default: [] })
  public events!: mongoose.Types.Array<[string, Date]>;

The complete code is:

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: mongoose.Types.Array<[string, Date]>, required: true, default: [] })
  public events!: mongoose.Types.Array<[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);

  • Related