realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Segmentation fault when adding an object with an incorrect schema to a List

Open michael-mckenna opened this issue 8 years ago • 4 comments

I was thinking of this being an enhancement where you guys can perhaps throw a specific error when this happens.

Code Sample

So if I have the schema:

const globalUserSchema = {  
        name: 'GlobalUser',  
        primaryKey: 'id',  
        properties: {   
            id: 'string',  
            email: {type: 'string', indexed: true},
            username: {type: 'string', indexed: true}
        }
    };

So if I have a globalUser object called "match" and I accidentally add brackets when I don't mean to: obj.users.push({match}); then I will get a segmentation fault. I fixed this by doing obj.users.push(match);.

Version of Realm and Tooling

  • Realm JS SDK Version: 1.10.0
  • Node or React Native: Node
  • Client OS & Version: CentOS 6, ROS 1.8.2

michael-mckenna avatar Aug 08 '17 17:08 michael-mckenna

@michael-mckenna I suppose obj has a schema with a list of GlobalUsers. Something like:

const Realm = require('realm');

const globalUserSchema = {  
    name: 'GlobalUser',  
    primaryKey: 'id',  
    properties: {   
        id: 'string',  
        email: {type: 'string', indexed: true},
        username: {type: 'string', indexed: true}
    }
};

const globalUserListSchema = {
    name: 'GlobalUserList',
    properties: {
        users: {type: 'list', objectType: 'GlobalUser'}
    }
};

Realm.open({schema: [globalUserSchema, globalUserListSchema]})
    .then(realm => {
        realm.write(() => {
            let obj = realm.create('GlobalUserList', {users: []});
            obj.users.push({username: 'John'});
        });
    });

Running that, I get Missing value for property 'GlobalUser.id' which is correct since id (and email) is not an optional property.

kneth avatar Aug 09 '17 09:08 kneth

Thanks for the response. I actually just edited the question because I was too hasty in opening this issue. I apologize for the inconvenience. I think the title still holds because if I do obj.users.push({match}); where extra brackets are added, that'll result in the list reading in a schema that doesn't match the one for a globalUser.

michael-mckenna avatar Aug 10 '17 19:08 michael-mckenna

@michael-mckenna Thanks for the clarification. I label it as an enhancement as I believe we should validate push's parameters more carefully.

kneth avatar Aug 11 '17 08:08 kneth

@kneth Sounds good!

michael-mckenna avatar Aug 13 '17 22:08 michael-mckenna