Realm-js: 'objectType' must be of type : string

Created on 22 Feb 2017  路  8Comments  路  Source: realm/realm-js

Hi im getting this error as soon as i call one on my methods inside realm.ts which is below

My models with realm schemas (models.ts)

import { ICity, IUser } from './interfaces'
import Realm , { ObjectSchema, PropertyType } from 'realm'

export class City implements ICity {

  // static get () { return realm.objects(City.schema.name) }
  static schema : ObjectSchema = {
    name: 'City',
    primaryKey : 'id',
    properties : {
      id : {type : 'string'},
      cityName : {type : 'string'},
      localImg : {type : 'string', optional: true},
      sourceLogo : {type : 'string', optional: true},
      cityCode : {type : 'list'},
    }
  }

  constructor(cityName?: string, cityCode?: string, sourceLogo?: string, localImg?: string) {
    this.cityName = cityName    
    this.cityCode = cityCode
    this.sourceLogo = sourceLogo
    this.localImg = localImg
  }

  public cityName: string;
  public localImg: string;
  public sourceLogo: string;
  public cityCode: string;
}

export class User implements IUser {

  //TODO: hash users pw
  static schema : ObjectSchema = {
    name: 'User',
    primaryKey : 'id',
    properties : {
      id : {type : 'string'},
      fullName : {type : 'string'},
      user : {type : 'string'},
      pw : {type : 'string'},
      cities : {type : 'list', objectType: 'City'},
    }
  }

  public fullName: string;
  public user: string;
  public pw: string;
  public cities: Array<ICity>;

  constructor(fullName?: string, user?: string, pw?: string, cities?: Array<ICity>) {
    this.fullName = fullName
    this.user = user
    this.pw = pw
    this.cities = cities
  }

}

Where realm happens (realm.ts)

import Realm from 'realm'
import { ListView } from 'realm/react-native'
import uuid from 'uuid'
import { User, City } from '../types/models';

//export const userItemDS = new ListView.DataSource({rowHasChanged: (r1, r2) => r1.id !== r2.id})

export const getUsers = () => {
  const users = realm.objects('User').sorted('id', true)
  console.log(users);
  return users
}

export const getUser = (id : string) => {
  const user = realm.objectForPrimaryKey(User, id)
  console.log(user);  
  return user
}

export const saveUser = ({fullName, user, pw} : User) => {
  realm.write(() => {
    realm.create(User.schema.name, {
      id: uuid.v1(),
      fullName,
      user,
      pw
    })
  })
}


const realm = new Realm({schema: [User, City]})

T-Help

Most helpful comment

ok its fixed, my problem was using the static property schema on my User object and passing the User constructor directly. Instead of doing :

const realm = new Realm({schema: [User, City]})

I have to do :

const realm = new Realm({schema: [User.schema, City.schema]})

Thanks everyone !

All 8 comments

?

Hi @wilomgfx. Thanks for reaching out. My apologies for the delay in someone getting back to you. The Javascript team is rather small and one of our JS engineers is currently on vacation. We'll try to get back to you as soon as we can!

@istx25 great. No problem. Thank you!

I would guess your problem is in saveUser. Most likely one of fullName, user, pw is undefined or another data type other than a string. Or perhaps you need to explicitly specify your key names in the dictionary passed to realm.create?

I will investigate in that direction. Thanks ! I'll post back with results in a few.

@wilomgfx is this still causing you trouble? Keep an eye out for #787 for tracking the error message.

Haven't got time to try it out. Had to fix some important issues. I will by trying tomorrow morning and ill post back if it worked or not. Thanks guys!

ok its fixed, my problem was using the static property schema on my User object and passing the User constructor directly. Instead of doing :

const realm = new Realm({schema: [User, City]})

I have to do :

const realm = new Realm({schema: [User.schema, City.schema]})

Thanks everyone !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timanglade picture timanglade  路  3Comments

kontinuity picture kontinuity  路  3Comments

emrehayirci picture emrehayirci  路  3Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments

gwanjar picture gwanjar  路  3Comments