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
Thanks a lot
Tobias
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:
npm install buffer (or Yarn)import { Buffer } from 'buffer'Buffer.from() on your data field. Using the original post, you'd call:const realmData = Buffer.from(plants[0].image)
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.
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
Bufferis not present in React Native. In order to get access to data fields and work around this issue, you can do this:npm install buffer(or Yarn)import { Buffer } from 'buffer'Buffer.from()on your data field. Using the original post, you'd call:realmDatawill contain your previously saved image data (or whatever binary you stored.) If you need it in ArrayBuffer format, you can just userealmData.bufferwhich will be aUint8Array.Hope this helps!