Couldn't update specific List[] properties


#1

My concern is that I’ve a schema:

class PriceAChangeSchema extends Realm.Object { }

PriceAChangeSchema.schema = {

    name: 'PriceAChange',
    properties: {
        icon: 'string?',
        cryptoName: 'string?',
        balance: 'string?',
        lastPrice: 'string?',
        change: 'string?',
        totalVal: 'string?',
    }
};

class PortfolioDataSchema extends Realm.Object { }

PortfolioDataSchema.schema = {

    name: 'PortfolioData',
    primaryKey: 'select',
    properties: {
        from: 'string?',
        select: 'string',
        apikey: 'string?',
        secretkey: 'string?',
        address: 'string?',
        logoLink: 'string?',
        priceandchange: 'PriceAChange[]'
    }
}

realm.write(() => {
            const ExchangeIns = realm.create('PriceAChange', {
                select: select, //primary key
                priceandchange: [], //want to change specifics
            }, true);

            ExchangeIns.priceandchange.push({
                balance: '0.004',
                lastPrice: '2.22',
                totalVal: '3.5',
            });
        });

I want to update 3 specific properties of PriceAChangeSchema so inside write, I’m pushing the new values inside the priceandchange array but the resultant is that the 3 specific values are changing but the rest of the values inside priceandchange i.e icon, cryptoName and change are becoming null… I really just want to update the 3 values and make the rest the same.


#2
realm.write(() => {

    // This creates a new PortfolioData Object
    // (you had this as PriceAChange but I think it's a typo and should be PortfoloData)
    const ExchangeIns = realm.create('PortfolioData', {
        select: select, //primary key
        priceandchange: [], //want to change specifics
    }, true);

    // this is creating a new PriceAChange and adding it to PortfolioData.priceandchange 
    ExchangeIns.priceandchange.push({
        balance: '0.004',
        lastPrice: '2.22',
        totalVal: '3.5',
    });

    // if you wanted to just update the price values of the first entry leaving the
    //  other values as they are you could just update them individually ...
    let toUpdate = ExchangeIns.priceandchange[0];
    toUpdate.balance = '0.005';
    toUpdate.lastPrice = '2.21';
    toUpdate.totalVal = '3.2';

 });

#3

Hi. I tried your solution but this isn’t working. It shows an error: “Cannot set property ‘balance’ of undefined”.

Basically I tried to console.log(ExchangeIns.priceandchange[0]), and it says undefined. The object is not present.


#4

Can anyone please help me out with this? This didn’t helped. :frowning: