0

I am building a React-Native app that uses Realm DB as the underlying database.

While implementing basic insertion methods I encountered exceptions when attempting to insert a new object with a unique identifier - while using UUID/ObjectId (same issue when using one or the other).

The weird thing is - when running in release mode - the exception disappears - and the insertion is successful!

Error log in debugger (both Metro and vscode debugger):

EconomeApp ERROR Log Start Fri Aug 26 2022 17:41:44 GMT+0300 (Israel Daylight Time)
Realm write ERROR: Error: CreditCard._id must be of type 'uuid', got 'object' ([object Object])
stack:
create@[native code]
anonymous@[native code]
write@[native code]
EconomeApp ERROR Log End

log in release mode:

 LOG  Running "EconomeApp" with {"rootTag":61}
 LOG  EconomeApp Log Start Fri Aug 26 2022 17:47:25 GMT+0300 (IDT)
created one card: d6ce1d14-0dc0-43db-8b6b-c4f04f85aa41
EconomeApp Log End
 LOG  EconomeApp Log Start Fri Aug 26 2022 17:47:25 GMT+0300 (IDT)
Realm written successfuly
EconomeApp Log End

Realm init method:

async _updateCollectionStructures() {
        try {
            this._realmObject = await Realm.open({
                path: Defenitions.databaseName,
                schema: [CollectionTypes.Assets, CollectionTypes.Category, CollectionTypes.ConstantExpense, CollectionTypes.CreditCard,
                CollectionTypes.DirectExpense, CollectionTypes.DynamicExpense, CollectionTypes.Income, CollectionTypes.Month, CollectionTypes.Preference,
                DataStructures.ExpenseData, DataStructures.IncomeData, DataStructures.LimitationGoalData],

                schemaVersion: Defenitions.databaseVersion,

                migration: (oldRealm, newRealm) => {
                    //future development...
                }
            });
        }
        catch (err) {
            console.error("Failed to open the realm db ", err.message);
        }
    };

write method:

/**
     * Writes to database
     * 
     * need to provide a callback with documents creation
     * @param {CallableFunction} func 
     */
    _writeToDB(func) {
        this._checkAndthrowNotInitializedErr();
        try {
            this._realmObject.write(func);
            Helper.log(`Realm written successfuly`);
        }
        catch (e) {
            Helper.logErr(`Realm write ERROR: ` + e + '\n' + 'stack:\n' + e.stack);
        }
    }

CreditCard class:

export class CreditCard {
    /**
     * 
     * @param {Realm.BSON.UUID} id 
     * @param {Date} lastPaidDate 
     * @param {Realm.BSON.Decimal128} cardLimit 
     * @param {string} lastFourDigits 
     * @param {boolean} isManualPayment 
     * @param {Number} dayCycle 
     */
    constructor(id, lastPaidDate, cardLimit, lastFourDigits, isManualPayment, dayCycle)
    {
        this._id = id;
        this.lastPaidDate = lastPaidDate;
        this.cardLimit = cardLimit;
        this.lastFourDigits = lastFourDigits;
        this.isManualPayment = isManualPayment;
        this.dayCycle = dayCycle;
    }

    static schema = {
        name: "CreditCard",
        primaryKey: '_id',
        properties: {
            _id: {type: "uuid", default: new UUID()},
            lastpaidDate: { type: "date", default: new Date() },
            cardLimit: { type: "decimal128", default: Defenitions.undefinedDecimalValue },
            lastFourDigits: { type: "string", default: Defenitions.undefinedString },
            isManualPayment: { type: "bool", default: Defenitions.defaultManualPayment },
            dayCycle: { type: "int", default: Defenitions.defaultDayCycle }
        },
    };
}

the attempted insertion method: where the exception is thrown

this._writeToDB(() => {
            let ob = new UUID();
            Helper.log(ob);
            let card1 = this._realmObject.create(CollectionTypes.CreditCard.schema.name, 
                new CollectionTypes.CreditCard(new UUID(), new Date(), new Realm.BSON.Decimal128("123.5123"), "1234", false, 15));
            Helper.log(`created one card: ${card1._id}`);
        });
Ethan K
  • 131
  • 1
  • 9
  • You said *same issue when using one or the other* - does that mean they result in the *exact* same error message? – Jay Aug 27 '22 at 13:08
  • Indeed - both in Chrome debugger and in VS code. – Ethan K Aug 27 '22 at 13:24
  • I am dubious. If you change the schema from this `_id: {type: "uuid", default: new UUID()}` to using an ObjectId, it shouldn't be throwing that same error. – Jay Aug 27 '22 at 14:05
  • I actually switched to ```ObjectId``` imported from ```Realm.BSON``` - and it still happens. – Ethan K Aug 27 '22 at 14:08

0 Answers0