Problem accessing an attribute dynamically inserted into a Sequelize "ready" object

Asked

Viewed 23 times

0

In a certain part of my code I have to set this field/attribute (actualValue) on an object coming from Sequelize (I can’t store the field in the database), but when doing mine if I cannot directly access the attribute as I do with the id, goal and state.

Is there any plausible reason for this to happen or some technique to circumvent it and make the code more standardized ?

const value = await movementsController.getSumMovs('F',fund.id);
fund.setDataValue('actualValue',value); 
//Funciona apenas se eu chamar fund.dataValues.actualValue
if (fund.actualValue >= fund.goal && fund.state !== 'C') await completeGoal(fund.id);

Object fund complete:

Fund {
  dataValues: {
    id: '8ce36534-8ead-4662-ab89-f57137a5cd7c',
    description: 'Descrição do UPDATE',
    goal: 6.5,
    state: 'C',
    createdAt: 2021-05-14T00:06:01.777Z,
    updatedAt: 2021-05-18T21:56:21.467Z,
    actualValue: 16.01
  },
  _previousDataValues: {
    id: '8ce36534-8ead-4662-ab89-f57137a5cd7c',
    description: 'Descrição do UPDATE',
    goal: 6.5,
    state: 'C',
    createdAt: 2021-05-14T00:06:01.777Z,
    updatedAt: 2021-05-18T21:56:21.467Z
  },
  _changed: Set(1) { 'actualValue' },
  _options: {
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    raw: true,
    attributes: [ 'id', 'description', 'goal', 'state', 'createdAt', 'updatedAt' ]
  },
  isNewRecord: false
}
  • "//Works only if I call Fund.dataValues.actualValue" if it works that way, why don’t you use it this way?

1 answer

0

In case, you have an object called Fund, to set the value just do this:

var fund = { ... }
fund.dataValues.actualValue = value;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.