0

So I've been adapting my code to the code of other team, but I'm getting from an event emitter old information.

In this function:

addEvent(event?: MatDatepickerInputEvent<Date>) {

  if (typeof event !== 'undefined') {

    this.refDate = event.value;
    this.refDate = new Date(this.refDate);
    this.startDate = new Date(this.refDate);
  } else if (typeof this.globals.contextDate === 'undefined') {
    this.refDate = new Date();
    this.refDate = new Date(this.refDate.getFullYear(), this.refDate.getMonth(), 0);
    this.startDate = new Date(this.refDate);
  } else {
    this.refDate = this.globals.refDate;
    this.startDate = new Date(this.refDate);
  }

  this.refDate.setHours(20);
  const dateStr = this.refDate.toISOString().substring(0, 10);

  this.globals.contextDate = dateStr;
  this.globals.refDate = this.refDate;

  // Atualiza o estado dos processos atraves da funcao do dashboard
  this.getProcessState(dateStr);

  // Transmite estados para os filhos
  this.refDateChange.emit({
    processClasses: this.processClasses,
    refDate: this.refDate
  });

  this.startDate.setMonth(this.refDate.getMonth());
  return this.startDate;
}

Detects when the datepicker changed It's value and is supposed to emit the newer information after that. But when I receive the info on the other component, the map object bring the old information.

When I console log:

this.captoolsComp.refDateChange.subscribe(data => {
  this.processClasses = data.processClasses;
  console.log(this.processClasses)
});

I shows this:

enter image description here

So you can see that the value of the key "Universo" (for example) it's not the same. What can I do to solve this?

Thank you so much.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Possible duplicate of [console.log() async or sync?](https://stackoverflow.com/questions/23392111/console-log-async-or-sync) – James Nov 21 '19 at 14:00
  • @James It could be that, but the values that I get inside that component are the ones displayed first and not the newer ones. – Fernando Caria Nov 21 '19 at 14:07
  • Easy to check, `console.log(JSON.stringify(this.processClasses));` should display "correct" data. – James Nov 21 '19 at 14:10
  • @James If I do that on the variable "data" - I reveive : {"processClasses":{},"refDate":"2019-04-30T19:00:00.000Z"} – Fernando Caria Nov 21 '19 at 14:20
  • you can try create a new object while transfer: ``` this.refDateChange.emit({ processClasses: Object.assign({}, this.processClasses), refDate: this.refDate }); ``` – shutsman Nov 21 '19 at 14:43
  • or const newObj = { processClasses: this.processClasses, refDate: this.refDate }; this.refDateChange.emit(newObj) – shutsman Nov 21 '19 at 14:46

0 Answers0