Realm-js: Realm returns empty data field

Created on 29 Nov 2016  路  5Comments  路  Source: realm/realm-js

Hi there,

Goals
I try am trying to save an image to realm and then read it.

Expected results
The database query should return the ArrayBuffer which represents the data of the image.

Actual results
Saving the ArrayBuffer seems to work as realm.create returns a valid object. When trying to read the image from the database, it only returns the id field, but the image data is empty.

Steps to reproduce
Save an ArrayBuffer and then read it.

Code

import Realm from 'realm';
import { encode, decode } from 'base64-arraybuffer';

const PlantSchema = {
  name: 'Plant',
  properties: {
    id:     'int',
    image:  'data'
  }
};

let realm = new Realm({schema: [PlantSchema]});

class Plant {

  getPlantImage(id) {
    const plants = realm.objects('Plant').filtered(`id == ${id}`);
    console.log(plants[0].image); // { '0': { id: 12, image: {} } }
    ...
  }

  save(id, imageData) {
    const decoded = decode(imageData); //base64 to ArrayBuffer
    realm.write(() => {
      let plant = realm.create('Plant', {
        id: id,
        image: decoded
      });
      console.log('saved: ' + plant.image); // prints saved: [object ArrayBuffer]
    });
  }
}

Versions

  • Realm: 0.15.0
  • react-native: 0.38.0
  • react: 15.4.1
  • Xcode: 8.1
  • iPhone 6 Simulator
  • OSX: 10.12.1

Thanks a lot
Tobias

O-Community T-Bug

Most helpful comment

So do data fields just not work in Realm JS at all? The issue is still present on 2.3, and it looks like this bug has been open for over a year.

Update: There doesn't seem to be any issues with the Node version, just React Native.

Update 2: The issue seems to be related to the fact that Buffer is not present in React Native. In order to get access to data fields and work around this issue, you can do this:

  1. First, install Buffer from NPM: npm install buffer (or Yarn)
  2. Import Buffer: import { Buffer } from 'buffer'
  3. Call Buffer.from() on your data field. Using the original post, you'd call:
const realmData = Buffer.from(plants[0].image)
  1. Now, realmData will contain your previously saved image data (or whatever binary you stored.) If you need it in ArrayBuffer format, you can just use realmData.buffer which will be a Uint8Array.

Hope this helps!

All 5 comments

I worked around by storing the image with the local file system and just saving the URI with realm.

I'm also having the same issue.

The issue is still there in realm 2.2.6.

So do data fields just not work in Realm JS at all? The issue is still present on 2.3, and it looks like this bug has been open for over a year.

Update: There doesn't seem to be any issues with the Node version, just React Native.

Update 2: The issue seems to be related to the fact that Buffer is not present in React Native. In order to get access to data fields and work around this issue, you can do this:

  1. First, install Buffer from NPM: npm install buffer (or Yarn)
  2. Import Buffer: import { Buffer } from 'buffer'
  3. Call Buffer.from() on your data field. Using the original post, you'd call:
const realmData = Buffer.from(plants[0].image)
  1. Now, realmData will contain your previously saved image data (or whatever binary you stored.) If you need it in ArrayBuffer format, you can just use realmData.buffer which will be a Uint8Array.

Hope this helps!

The test testRealmCreateOrUpdate indicates that binary data is support and it works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ugendrang picture ugendrang  路  3Comments

max-zu picture max-zu  路  3Comments

kontinuity picture kontinuity  路  3Comments

Chris-Petty picture Chris-Petty  路  3Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments