question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bad date format with SQL database

See original GitHub issue

With SQL database (PostgreSQL), feathers-auth-management doest not work out of the box, because of the use of Date.now() instead of new Date().

I got errors like this:

{
  "name": "BadRequest",
  "message": " date/time field value out of range: \"1516020161065\"",
  "code": 400,
  "className": "bad-request",
  "errors": {}
}

Maybe you should consider using new Date() instead of Date.now(), to use actual Date.

Regards.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
hashcutdevcommented, Jun 4, 2020

I realize this is a few years later but I ran into the same problem and have a simpler fix. The above solution from @morenoh149 works but then the data is stored as an int rather than the proper datetime format. I’m using Objection, and it was easy to do the data conversion in the Objection model file thusly:

users.model.js:

...
class users extends Model {
    static get tableName() {
        return 'users';
    }

    static get jsonSchema() {
        return {
            type: 'object',
            required: ['password'],

            properties: {
                email: { type: ['string', 'null'] },
                password: 'string',

                auth0Id: { type: 'string' },

                googleId: { type: 'string' },

                facebookId: { type: 'string' },

                twitterId: { type: 'string' },
                isVerified: { type: 'boolean' },
                verifyToken: { type: 'string' },
                verifyExpires: { type: 'date' },
                verifyChanges: { type: 'object' },
                resetToken: { type: 'string' },
                resetExpires: { type: 'date' },
            },
        };
    }

    $beforeInsert() {
        // eslint-disable-next-line no-multi-assign
        this.createdAt = this.updatedAt = new Date().toISOString();
        if (typeof(this.resetExpires) === 'number') {
            this.resetExpires = new Date(this.resetExpires).toISOString();
        }
        if (typeof(this.verifyExpires) === 'number') {
            this.verifyExpires = new Date(this.verifyExpires).toISOString();
        }
    }

    $beforeUpdate() {
        this.updatedAt = new Date().toISOString();
        if (typeof(this.resetExpires) === 'number') {
            this.resetExpires = new Date(this.resetExpires).toISOString();
        }
        if (typeof(this.verifyExpires) === 'number') {
            this.verifyExpires = new Date(this.verifyExpires).toISOString();
        }
    }
}
...

The main part is the $beforeInsert and $beforeUpdate functions, which check to see if the verifyExpires or resetExpires fields are numbers, and then recasts them to the proper Date value. This works and leaves your postgresql database with the proper field types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bad date format change from string to date in Bigquery
NB: I did not apply an ORDER BY clause to the Results hence the data is not ordered by any specific column in...
Read more >
Bad date format with SQL database · Issue #86 - GitHub
Hi, I got this error error: RequestError: Operand type clash: bigint is incompatible with date when using the date type in SQL Server...
Read more >
Access front - SQL back wrong date format issue - MSDN
In SQL the date format is yyyy/mm/dd and on my Access backend and frontend the date format is mm/dd/yyyy. But as the queries...
Read more >
Caught bad date format with exact error - Oracle Communities
As an aside, why are your trying to assign a timestamp to a DATE? A DATE only resolves down to a second, and...
Read more >
How to Get SQL Server Dates and Times Horribly Wrong
The numeric format is considered to be a more universal way to pass in time-date data. After all, that's how SQL Server returns...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found