Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3.7.0-alpha.2] CreatedAt must be of type 'date', got 'object' #2755

Closed
bimusiek opened this issue Mar 17, 2020 · 27 comments
Closed

[v3.7.0-alpha.2] CreatedAt must be of type 'date', got 'object' #2755

bimusiek opened this issue Mar 17, 2020 · 27 comments

Comments

@bimusiek
Copy link
Contributor

bimusiek commented Mar 17, 2020

Goals

Save model on node server with createdAt being new Date()

Expected Results

Model is saved

Actual Results

Got error

Error: LicenseRequestModel.createdAt must be of type 'date', got 'object' (Tue Mar 17 2020 20:03:14 GMT+0100 (Central European Standard Time))

Steps to Reproduce

Try to create new model on node server with new Date()

Code Sample

  static create<T>(this: RealmModelThis<T>, realm: Realm, props: any): T {
    return realm.create<T>(
      this.schema.name,
      {
        ...props,
        createdAt: new Date(),
        modifiedAt: new Date()
      },
      Realm.UpdateMode.All
    );
  }

Version of Realm and Tooling

  • Realm JS SDK Version: v3.7.0-alpha.2
  • Node or React Native: Node v13.3.0
  • Client OS & Version: jest
@bimusiek
Copy link
Contributor Author

Workaround for now was to use int and store date as a number. 😢

@kneth
Copy link
Contributor

kneth commented Mar 18, 2020

@bimusiek Could you post the schema?

@kneth kneth added the T-Help label Mar 18, 2020
@kneth
Copy link
Contributor

kneth commented Mar 18, 2020

@bimusiek We have release v5.0.0. Please try to upgrade.

@bimusiek
Copy link
Contributor Author

I will check, thanks.

And do you know @kneth when are your team planning to release 4.0.0 beta with Node 13 support? 3.7.0 seems to work with newest node already.

@bimusiek
Copy link
Contributor Author

@kneth With 5.0.0 there is the same issue.
Error: LicenseRequestModel.subscriptionExpiresAt must be of type 'date?', got 'object' (Thu Mar 12 2020 11:48:52 GMT+0100 (Central European Standard Time))

Schema:

  public static schema = {
    ...BaseModel.schema,
    name: 'LicenseRequestModel',
    primaryKey: 'licenseId',
    properties: {
      ...BaseModel.schema.properties,
      licenseId: 'string',
      token: { type: 'string', default: '' },
      email: { type: 'string', default: '' },
      status: { type: 'int', default: LicenseRequestStatus.INITIALIZED },
      type: { type: 'int', default: LicenseRequestType.WEB },
      hasSubscription: { type: 'bool', default: false },
      subscriptionExpiresAt: 'date?'
    }
  };

export abstract class BaseModel {
  public static schema = {
    properties: {
      createdAt: 'int',
      modifiedAt: 'int'
    }
  };

@kneth
Copy link
Contributor

kneth commented Mar 19, 2020

@bimusiek Thank you for the update. Could you try subscriptionExpiresAt: { type: 'date', optional: true} instead. If that works, we have a bug in the schema parser, and if it does, the bug is in the object creation code.

@bimusiek
Copy link
Contributor Author

Unfortunately, the same issue.

@xmedeko
Copy link

xmedeko commented Mar 24, 2020

We use JS, Realm 5 and date, date? are ok. E.g.

const schema = [
    {
        name: "Something",
        properties: {
            data: "string",
            createdAt: "date",
            changedAt: "date?"
        }
    }];
Realm.open(schema);

Then create, update etc. is OK.

@kneth
Copy link
Contributor

kneth commented Mar 26, 2020

It looks like the issue is how we interact with classes rather than a Date issue. @bimusiek Can you try to write your schema as suggested in #2755 (comment)

@bimusiek
Copy link
Contributor Author

bimusiek commented Apr 3, 2020

@kneth The schema I pass to Realm constructor is

  const realmConfig = user.createConfiguration({
    schema,
    schemaVersion,
    sync: {
      url: `~/app`,
      fullSynchronization: true
    }
  });
  const realm = new Realm(realmConfig);

where schema is:

export const schema = [
  ConfigRealmModel.schema,
  CollectionModel.schema,
  CollectionItemModel.schema,
  ImporterAuthenticationModel.schema,
  TransferModel.schema,
  TransferCollectionModel.schema,
  TransferCollectionItemModel.schema,
  LicenseRequestModel.schema
];
export const schemaVersion = 1;

So as I understand, it is done in the same manner by passing JS object directly.
Also, it worked with 4.0.0 alpha.

I forgot to mention that I use in memory realm for Jest tests, maybe there is an issue?

@voidrender
Copy link
Contributor

I forgot to mention that I use in memory realm for Jest tests, maybe there is an issue?

I'm also running into this issue only while using an in-memory Realm for Jest tests.

@mtford90
Copy link

Facing this issue myself with jest tests using inMemory Realm.

Works fine on 3.6.5

@d3v2a
Copy link

d3v2a commented Apr 20, 2020

I'm also running into this issue only in jest test (in-memory or not)

the same code directly in node work fine

to get it working in jest:

new Date().toISOString();

@thomasarogers
Copy link

Same having this issue in Jest only when run inmemory

@bimusiek
Copy link
Contributor Author

@kneth I would like to propose changing label from T-Help to T-Bug

@aj3x
Copy link

aj3x commented Jul 8, 2020

This may be related, obj.createdAt instanceof Date returns false. When using Realm 3.6.5 this returned true. Debugging reveals all the functions of Date are there.

@martnst
Copy link

martnst commented Jul 11, 2020

I am also facing this issue with

  • realm 5.0.5 + jest 24.9.0
  • realm 6.0.2 + jest 26.1.0

@bharadwajagali
Copy link

Hi @kneth any update on the above issue, I am also facing same issue with below configuration.

Works fine on 3.6.5.

  • Realm 6.0.4 + Jest 25.1.0
  • Realm 6.0.4 + Jest 26.3.0
  • React-native version 0.61.2
  • Node js Version 10.18.0

@kneth
Copy link
Contributor

kneth commented Aug 31, 2020

@bharadwajagali No real progress. Mostly because we are migrating to MongoDB Realm Cloud as our future platform.

@blagoev blagoev mentioned this issue Sep 17, 2020
@blagoev
Copy link
Contributor

blagoev commented Sep 17, 2020

this was fixed and will be part or our next release

@McFlyssss
Copy link

Hey guys, I'm facing the same issue with Realm 6.1.5 when running on Jest. This was supposed to be fixed, right @blagoev?

Thanks in advance!

@kneth
Copy link
Contributor

kneth commented Dec 9, 2020

@McFlyssss Yes, it should be fixed.

Could you try to build from source: npm install --build-from-source --save realm@6.1.5

Currently we support node v10 which has end-of-life late April next year. After that we can bump the N-API version to 5. But building from source using node v12 (or later), you will N-API version 5 (or later), and my hypothesis is that another code plan is exercised with version 5 (or later).

@McFlyssss
Copy link

Dear @kneth ,

Thank you so much for your answer. Sorry I could not get back to it earlier.
Unfortunately, this raises the same exception. Any other idea?

@kneth
Copy link
Contributor

kneth commented Dec 14, 2020

@McFlyssss It might be related to Jest (see jestjs/jest#2549) and unfortunately I don't have a simple solution (but it might be worth to read #2490 (comment)).

@McFlyssss
Copy link

McFlyssss commented Dec 15, 2020

FYI I tried with Realm v10 and having the exact same problem. I'll take a look at those links you shared @kneth

@kneth
Copy link
Contributor

kneth commented Dec 16, 2020

@McFlyssss When you have had time to investigate, you are welcome to open a new issue with your observations.

@McFlyssss
Copy link

So finally got it working.
Updating the node version made it work. I was running on 10.16.3 and now I'm at 10.22.1.
It seems there are some changes in regards to dates on 10.17: nodejs/node#25917

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests