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

Using ObjectClass throws error upon write in Node.js. #998

Closed
tybro0103 opened this issue May 4, 2017 · 12 comments
Closed

Using ObjectClass throws error upon write in Node.js. #998

tybro0103 opened this issue May 4, 2017 · 12 comments

Comments

@tybro0103
Copy link

tybro0103 commented May 4, 2017

The following works fine on ReactNative, but throws an error in Node.js.

  • realm-js v1.2.0
  • Node v7.0.0

Error:

realm.write(() => {
      ^
TypeError: Class constructor Car cannot be invoked without 'new'

Code:

const Realm = require('realm');

const CarSchema = {
  name: 'Car',
  properties: {
    color: 'string',
  },
};

class Car {};

Car.schema = CarSchema;

const realm = new Realm({
  path: 'foo.realm',
  schema: [Car],
});

realm.write(() => {
  realm.create('Car', {color: 'red'});
});

I have also tried making these changes, but had the same results:

  • realm.create(Car, {color: 'red'});
  • class Car extends Realm.Object {};
@vikkio88
Copy link

vikkio88 commented May 4, 2017

I use it this way

realm.create('Car', {color: 'red'});

with Car being a string representing the object name

@tybro0103
Copy link
Author

@vikkio88 not sure I understand... I believe that's the same as my example? Are you using that node without error? If so, what version of node and what version of realm-js?

@vikkio88
Copy link

vikkio88 commented May 4, 2017

Your example

realm.create(Car, {color: 'red'});

My example

realm.create('Car', {color: 'red'});

'Car' is wrapped in quote, is a string, not the model object

@tybro0103
Copy link
Author

I have both of those in my examle... both of them work in React Native, but both of them result in the same error on Node.

@vikkio88 Are you running this on Node, or?

@vikkio88
Copy link

vikkio88 commented May 4, 2017

Oh sorry didn't get what you meant, was react native

@petebacondarwin
Copy link

Is this a node 7 related thing? I.E. is it because native classes in node are treated unlike regular functions?

@blagoev
Copy link
Contributor

blagoev commented May 22, 2017

Hi, sorry for the late reply.
We don't support ES6 classes natively on node. On react that code is being transpiled before running the app so it works there. On node you will need to transpile your code before running it.

cheers

@blagoev blagoev self-assigned this May 22, 2017
@tybro0103
Copy link
Author

tybro0103 commented May 23, 2017

@blagoev Ah, I see. So much of it worked in Node 7 without transpiling I didn't think of that.

I know it's a different story for front-end JS, but I'd assume a lot of people doing server-side Node aren't transpiling. Seems like it might be ideal to not require it for a lib like this?

@blagoev
Copy link
Contributor

blagoev commented May 23, 2017

You could transpile or you could write the above sample with

......
var Car = function Car() {
}

Car.schema = CarSchema;
.........

@tybro0103
Copy link
Author

I'd think that if the realm-js lib is expected to be transpiled, then even if you can get it to work without transpiling, it'd still be unsafe. Because even a patch version bump could contain a change that no longer works in your setup without transpiliation.

Since realm-js supports both React Native and Node, it might be worth considering pre-transpiling the npm releases. Since the newer versions of Node support most of ES6, many Node users may find it ideal to not transpile their projects.

@blagoev
Copy link
Contributor

blagoev commented Jun 3, 2017

True. ES6 is in our radar. will move this to a backlog for future consideration

@fronck
Copy link
Contributor

fronck commented Feb 17, 2022

I'm closing this issue as we no longer support RealmJS < v6. I haven't been able to reproduce the issue on v6 or v10.

@fronck fronck closed this as completed Feb 17, 2022
@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

8 participants