Typeerror: sheets.foreach is not a Function typescript [on hold]

Asked

Viewed 399 times

1

I’m trying to display the data of some interns using a card library made by another developer. The application was all done in Angular (including lib). A few weeks ago, everything was working, but recently I went to test the application and an error appears in Typescript.

I have this function that mounts the cards on the screen:

// Monta a visualização dos cards
  folhaDePagamentoEstagiariosToItemCard(folhas) {
    const dados: any[] = [];

    // Pipe para a formatação da moeda
    const pipe = new CurrencyPipe('pt');

    folhas.forEach(dado => {
      const item: ItemCard = new ItemCard();
      item.cabecalho = dado.Nome;
      item.subtitulo = 'Lotação: ' + dado.Lotacao;
      item.resumo = <string>this.sanitizer.bypassSecurityTrustHtml(
        '<table>' +
        '<tr><th>Estabelecimento:</th><td>' + dado.Estabelecimento + '</td></tr>' +
        '<tr><th>Início do Contrato:</th><td>  ' + dado.Inicio + '</td></tr>' +
        '<tr><th>Término do Contrato:</th><td> ' + dado.Termino + '</td></tr>' +
        '<tr><th>Bolsa Auxílio:</th><td>' + pipe.transform(dado.Proventos, 'R$')  + '</td></tr>' +
        '</table>');
      dados.push(item);
    });
    return dados;
  }

The card summary is where the trainees' data appears. It’s a table that uses the pattern that was set in the lib I’m using.

But I get the following error in the browser console:

core.js:14597 ERROR TypeError: folhas.forEach is not a function
    at EstagiariosComponent.push../src/app/estagiarios/estagiarios.component.ts.EstagiariosComponent.folhaDePagamentoEstagiariosToItemCard (estagiarios.component.ts:62)
    at SafeSubscriber._next (estagiarios.component.ts:51)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
    at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
    at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
    at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)
    at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)

Rather the function forEach was working. Now I have this problem. I am grateful if someone can help me!

  • from a sheet log console, probably not an array

  • Okay. Try changing the forEach for for, besides Array.ForEach be about 95% slower than for. I’ll edit the answer.

2 answers

0

Usually this type of error arises because the variable leaves is not an array. And in your case to solve this, you have to check if the variable is an array, and if not have to initialize.

Use the syntax of the standard parameter instead of changing the arguments of the function.

// muito ruim
function folhaDePagamentoEstagiariosToItemCard(folhas) {
  folhas = folhas || {};
  // ...
}

// ainda ruím
function folhaDePagamentoEstagiariosToItemCard(folhas) {
  if (folhas === void 0) {
    folhas = {};
  }
  // ...
}

// Bom
function folhaDePagamentoEstagiariosToItemCard(folhas = {}) {
  ...
  for(let i = 0; i < folhas.length; i++){
    ...
    var dado = folhas[i];
    ...
}
  • Hello, I tried to apply this solution, but it did not work. And I use the leaf functionDePagamentoEstagiariosToItemCard(sheets){} on another component of my application and it works without initializing the leaf variable.

  • Okay. Try changing the forEach for for, besides Array.ForEach be about 95% slower than for. I’ll edit the answer.

0

The problem is because the leaves may not be an array.

Initializes in the function thus:

folhaDePagamentoEstagiariosToItemCard(folhas = []) {
  • Same thing as the comment above. I tried applying, but it didn’t solve my problem. I still have the same error.

Browser other questions tagged

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