Unable to insert moment datetime in mysql database


#1

I’m trying to insert a moment date time field to mysql database.
Here is code :

const validTill = moment().add(5, 'hours').format("YYYY-MM-DD HH:mm:ss")
const accessSession = await Session.findOrCreate({
      'userId': user.id ,
     'deviceType' : deviceType 
})
 accessSession.validTill = validTill
 await accessSession.save()
     

This is the error i get

code: 'ER_BAD_FIELD_ERROR',
0|server   |   errno: 1054,
0|server   |   sqlMessage: 'Unknown column \'_isUTC\' in \'field list\'',
0|server   |   sqlState: '42S22',
0|server   |   index: 0,
0|server   |   sql:
0|server   |    'update `access_sessions` `accessToken` = \'token\', `validTill` = `_isAMomentObject` = true, `_isUTC` = false, `_pf` = \'[object Object]\', `_locale` = \'[object Object]\', `_d` = \'2019-01-19 13:17:36.361\', `_isValid` = true, `created_at` = \'2019-01-19 07:50:28\', `updated_at` = \'2019-01-19 08:17:36\' where `id` = 1' }

This is my migration :

this.create('access_sessions', (table) => {
      table.increments()
      table.integer('userId').notNullable()
      table.integer('deviceType').notNullable()
      table.string('accessToken')
      table.datetime('validTill')
      table.timestamps()
    })

I’m running Ubuntu 18.04 with mysql Ver 14.14 Distrib 5.7.24.
What am i doing wrong ?


#2

Hey @sayoojkrishnan! :wave:

Seems that it’s dumping the whole moment object instead of the formatted date.
Please, try with the following code:

const validTill = moment().add(5, 'hours').format("YYYY-MM-DD HH:mm:ss").toString()

#3

:wave: That didn’t work. :sleepy: The same code works fine when i use Postgresql .


#4

Are you defining the validTill field as one of the model’s date fields?

static get dates () {
  return super.dates.concat(['validTill'])
}

I’d also try just using .format() instead of specifying a particular format string, that should spit out the appropriate iso date time string with timezone offset, ie:
2019-01-20T04:00:14+00:00


#5

Thanks @willvincent :metal: The issue was solved after reinstalling nodeJS and mysql npm package.