ArsenyYankovsky / typeorm-aurora-data-api-driver

A bridge between TypeORM and Aurora Data API
MIT License
175 stars 38 forks source link

date.getUTCFullYear is not a function #74

Closed davingreen closed 3 years ago

davingreen commented 3 years ago
TypeError: date.getUTCFullYear is not a function
      at dateToDateTimeString (/opt/nodejs/node_modules/typeorm-aurora-data-api-driver/dist/typeorm-aurora-data-api-driver.umd.js:943:25)
      at PostgresQueryTransformer.preparePersistentValue (/opt/nodejs/node_modules/typeorm-aurora-data-api-driver/dist/typeorm-aurora-data-api-driver.umd.js:1181:32)
      at DataApiDriver.preparePersistentValue (/opt/nodejs/node_modules/typeorm-aurora-data-api-driver/dist/typeorm-aurora-data-api-driver.umd.js:1363:42)
      at AuroraDataApiPostgresDriver.preparePersistentValue (/opt/nodejs/node_modules/app/node_modules/typeorm/driver/aurora-data-api-pg/AuroraDataApiPostgresDriver.js:72:28)
      at /opt/nodejs/node_modules/app/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:365:57
      at Array.forEach (<anonymous>)
      at /opt/nodejs/node_modules/app/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:353:25
      at Array.forEach (<anonymous>)
      at UpdateQueryBuilder.createUpdateExpression (/opt/nodejs/node_modules/app/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:347:85)
      at UpdateQueryBuilder.getQuery (/opt/nodejs/node_modules/app/node_modules/typeorm/query-builder/UpdateQueryBuilder.js:45:21)
}

When trying to update:

  @Column({ nullable: true, type: 'timestamp' })
  public scheduledToSendAt?: Date

using

const announcement: IUpdateAnnouncement = {
    ...data,
    updatedAt: new Date(),
  }
const announcementRepo = connection.getRepository(Announcement)
await announcementRepo.save(announcement)
davingreen commented 3 years ago

I resolved this by transforming the scheduledToSendAt to date from a string. Apparently, the data api driver causes an issue with TypeORM's automatic casting of date strings to dates (maybe?)

Anyway, I've got a workaround -- thanks!

tgechev commented 2 years ago

Hello @davingreen,

Could you please confirm whether I have understood your workaround properly?

You had your initial property set up as Date type:

@Column({ nullable: true, type: 'timestamp' })
public scheduledToSendAt?: Date

You got the error and you changed it to a string type, thus fixing your problem:

@Column({ nullable: true, type: 'timestamp' })
public scheduledToSendAt?: string

I am asking because I am facing the exact same error, however, all date properties in my class are already strings like so:

@Column('date')
date1: string;

@Column({ type: 'timestamptz', nullable: true })
date2?: string;

And I am trying to save a record in the db like so:

const record = new Entity({
      date1: new Date().toISOString(),
      date2: new Date().toISOString(),
});
const errors = await validate(record);
if (errors.length > 0) {
  console.log('Invalid record!');
} else {
  console.log('Saving new record...');
  await record.save();
}

of course without success because of the TypeError: date.getUTCFullYear is not a function

Thanks in advance! :)

davingreen commented 2 years ago

Hi @tgechev,

Running latest updates, I was able to change the types back to Date. You can then use a JS Date object (instead of new Date().toISOString(), just new Date().

I've found that the TypeError: date.getUTCFullYear is not a function typically occurs when TypeORM is expecting a Date object, but receiving a string.

Hope that helps!