React-Native sdk "no internal field" when creating a new object

Hi!
I’m using the WildAid O-FISH post to create a similar project using react-native SDK with flexible sync.

I’m trying to create a Photo object but I get a “no internal field” error.

Here’s my Photo model

export class Photo extends Realm.Object {
    _id;
    user;
    result;
    picture;
    pictureUrl;
    annotation;
    userInputs;
    createdAt;
    projectId;

    static schema = {
        name: "Photo",
        properties: {
            _id: "objectId",
            user: "string",
            result: "Result?",
            picture: "data?",
            pictureUrl: "string?",
            annotation: "string?",
            userInputs: "{}?",
            createdAt: "date",
            projectId: "objectId",
        },
        primaryKey: "_id",
    };
}

export const ResultSchema = {
    name: "Result",
    embedded: true,
    properties: {
        traits: "Trait{}?",
        errors: "{}?",
        score: "Score?",
    },
};

export const TraitSchema = {
    name: "Trait",
    embedded: true,
    properties: {
        value: "float?",
        unit: "string",
    },
};

export const ScoreSchema = {
    name: "Score",
    embedded: true,
    properties: {
        score: "int",
        total: "int",
        minusPoints: { type: "MinusPoint[]", default: [] },
    },
};

export const MinusPointSchema = {
    name: "MinusPoint",
    embedded: true,
    properties: {
        value: "int",
        reason: "string",
    },
};

And here’s how I’m creating a new photo

// Write transaction omitted
// Read a local image and convert it to base64
const picture = await readFile(path, "base64");
// Convert the base64 image to Buffer
const binaryBuffer = Buffer.from(picture, "base64");

const newPhoto = realm.create("Photo", {
    _id: new Realm.BSON.ObjectId(),
    user: user.profile.email,
    userInputs: section.userInputs,
    createdAt: new Date(),
    projectId: new Realm.BSON.ObjectId(projectId),
    annotation: "someString",
    picture: binaryBuffer,
})

I feel like the problem might come from the picture property. I read in the doc that *data* type maps to ArrayBuffer which is what a Buffer is. Maybe it’s another field causing the problem but I really don’t know which one.

Thanks in advance!

Is that intentional?

Yes, userInputs is an optional dictionary of mixed values.
Is this syntax forbidden?

No - it’s fine. Just wanted to ensure that was actually what you meant to do. To narrow the issue, I would comment out some of the properties and add them back in until the crash occurs.

On another note, Realm is generally not well suited for storing blob type data like images as they can often go beyond what can be stored in a single property. 16Mb is the limit.

While I know it’s done in the WildAid O-FISH app, they have limits in place to ensure that doesn’t happen. There are a number of other options for storing images, and you can keep the url of that within Realm.

Thank your for your advice!

As the error isn’t very specific I’ve tried commenting out a few properties especially picture and date being the two with the most “unsual” type.
For instance, is a Buffer a correct value for picture: "data" in my schema?

One thing I didn’t mention is that when I try to create a photo I get an error but a document is still created and replicated to the server. Maybe the error comes from the server?
I have a trigger running when a new Photo document is added (to remove picture and upload it to S3)

Here’s what the created photo looks like

I don’t understand why the projectId isn’t set. I didn’t create a relationship I just typed projectId: "ObjectId" and I’m giving an ObjectId instance to the property

PS: I’m aware of Mongo 16MB limit, I’ve implemented the same trigger as the WildAid O-FISH app to remove the photo

The problem came from section.userInputs, I forgot to include its definition in my first post
const section = realm.objectForPrimaryKey("Section", new Realm.BSON.ObjectId(sectionId));.

I replaced userInputs: section.userInputs with userInputs: section.userInputs.toJSON()

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.