question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Realm returns empty data field

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
alaxcommented, Apr 22, 2018

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!

2reactions
spencexyzcommented, Apr 19, 2017

I’m also having the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm.objects(Type) return empty objects - Stack Overflow
the data is already in the database , I can see the data in Realm browser but when I try to get it...
Read more >
MongolDB Realm is returning an empty realm
Hello everyone, Lately I've been looking into using SwiftUI to implement my app, I used the guidelines from this repository: ...
Read more >
Realm Database on Android: Getting Started
Learn how to use Realm database on Android for data persistence. You'll learn how to add it to your android app and utilize...
Read more >
RealmResults (Realm 1.1.0)
Returns an Rx Observable that monitors changes to this RealmResults. double, average(java.lang.String fieldName). Returns the average of a given field.
Read more >
2272271 - No Realm is available - Unable to select any Object ...
When creating an Advanced Reporting query, the drop down shows 'No Realm is ... No Realm is available - Unable to select any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found