0

When I output the Array in my console, it looks like this:

enter image description here

for (let index = 0; index < employeeList.length; index++) 

this for loop doesn't work, because length is 0. I tried different methods, like

Object.keys(myObj).length; 

or others from Object.

I don't know how to iterate through this Object Array. I also don't get how I could access an index value through: for (var key in employeeList) and get for example the employeeId.

Could you help me?:)

Full Code :

public selectWorkingHourListForCurrentMonthAndEveryEmployee(
month: Date
 ): Observable<WorkingHours[]> {
const firstDay = new Date(month);
firstDay.setDate(1);
const lastDay = new Date(
  firstDay.getFullYear(),
  firstDay.getMonth() + 1,
  0
);
const employeeList: Employee[] = [];
let workingHoursSummedUp: WorkingHours[] = [];

this.employeeService.getAllEmployees().then((res) => {
  employeeList.push(...res);
});
console.log(employeeList)



if (employeeList) {
  for (let index = 0; index < employeeList.length; index++) {
    this.workingHoursService
      .getWorkingHours(
        employeeList[index].employeeId,
        firstDay,
        lastDay,
        true
      )
      .then((res) => {
        workingHoursSummedUp.push(...res);
      });
  }
}
der papa
  • 35
  • 5
  • Seems like `length` is 4. Why do you say it's 0? – Konrad Feb 23 '23 at 14:08
  • when i do console.log(empoyeeList.length), i get 0 – der papa Feb 23 '23 at 14:12
  • 1
    I guess that you try to access employeeList before this array is initialized. You should share of your code in order for us to be able to understand where is your bug – TheTisiboth Feb 23 '23 at 14:12
  • No I access it after I log it in the console. And as you can see, in the console there is the list fully initialized – der papa Feb 23 '23 at 14:13
  • Does this answer your question? [Is Chrome’s JavaScript console lazy about evaluating objects?](https://stackoverflow.com/questions/4057440/is-chrome-s-javascript-console-lazy-about-evaluating-objects) – Konrad Feb 23 '23 at 14:14
  • `console.log` shows the object in the current state, not from when it was called. Use `console.log(JSON.stringify(object))` if you want to know how the object looks or use a debugger – Konrad Feb 23 '23 at 14:15
  • In your code `console.log` runs before the promise is fulfilled. – Konrad Feb 23 '23 at 14:16
  • Hmm, youre right. When I JSON.Stringify it, it is empty – der papa Feb 23 '23 at 14:21
  • I edited my code. You can see the rest. Is there a way to wait for it to be fulfilled? – der papa Feb 23 '23 at 14:22

1 Answers1

1

There are two things you have to know:

  1. By default code will not stop and wait for promises
  2. There is no time travel

So doing things like this won't work:

const x = []
doSomething().then(res => x.push(...res))
console.log(x)

In the above code console.log(x) will happen before the function in then

The easiest solution is to use async and await

public async selectWorkingHourListForCurrentMonthAndEveryEmployee(
  month: Date
): Observable<WorkingHours[]> {
  const firstDay = new Date(month);
  firstDay.setDate(1);
  const lastDay = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 0);

  let workingHoursSummedUp: WorkingHours[] = [];

  const employeeList: Employee[] = this.employeeService.getAllEmployees();
  for (let index = 0; index < employeeList.length; index++) {
    const res = await this.workingHoursService.getWorkingHours(
      employeeList[index].employeeId,
      firstDay,
      lastDay,
      true
    );
    workingHoursSummedUp.push(...res);
  }
  console.log(workingHoursSummedUp);
}
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • But I have to use Promise instead of Observable, right? – der papa Feb 23 '23 at 14:23
  • @derpapa I don't see a part of your code that is using an observable. You are using `.then`, but observables usually use `.subscribe`. You can't use await with an observable, you will need to use pipes or just make consequent operations in the `subscribe` handler – Konrad Feb 23 '23 at 14:24