Tim Denison Tim Denison -4 years ago 197
Javascript Question

Realm Error: "JS value must be of type: number"

I've had a prototype of an app running great with a simple, single-scheme realm. But now I want to add a couple of other schema and let one schema contain a list of the others.

Ultimately, I get an error when executing the

let realm = new Realm({schema: MySchemas});


line. Error is: "JS value must be of type: number". Here's how I've got things setup.

Schema.js

'use strict';

let currentSchemaVersion = 0;

const Schema1 = {
name: 'Type1',
primaryKey: 'id',
properties: {
id: {type: 'string', indexed:true},
propOne: 'string',
propTwo: 'string',
propThree: 'int',
propFour: {type: 'list', objectType: 'Type2'},
propFive: {type: 'list', objectType: 'Type3'}
}
};

const Schema2 = {
name: 'Type2',
primaryKey: 'id',
properties:{
id: 'string',
prop1: 'string'
}
};

const Schema3 = {
name: 'Type3',
primaryKey: 'id',
properties:{
id: {type:'string', indexed:true},
propOne: {type:'string', indexed:true},
}
}

export const MySchemas = [Schema1, Schema2, Schema3];


Then in the referencing file, call it MyComponent.js

'use strict';

import Realm from 'realm';
import * as MySchemas from './Data/Schema.js';

let realm = new Realm({schema: MySchemas}); //BARRRFFFF!


The only number in the whole lot is propThree from Type1, right? So I figure the problem is somehow related, but I've got no visibility into the internals so I don't know what the deal is. I've tried with and without a default on that property. Either way it's no go.

Thanks a ton for any insight. And no, unfortunately, How to add a nested List of objects in Realm "Error: JS value must be of type: object" is not helpful.

Edit: I just removed the nested lists AND the only property in the whole show that was a number (propThree) and I still get the same error.

Answer Source

Chalk it up to being fairly new to JS, I guess. I've been able to get things working by changing how I import the schemas into MyComponent.js.

Instead of

import * as MySchemas from './Data/Schema.js';

if I go with

import {MySchemas} from './Data/Schema.js';

things work. Alternatively, if Schema.js uses export default, then I believe the original import will work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download