0

I am getting the following error in react 'Unhandled Rejection (TypeError): slots.every(...) is not a function' and I can't seem to solve it.

componentWillMount() {
  axios.get(API_BASE + `api/retrieveSlots`).then(response => {
    console.log("response via db: ", response.data);
    this.handleDBReponse(response.data);
  });
}

for (let day in schedule) {
  let slots = schedule[day];
  if (slots.length) slots.every(slot => slot === true)(schedule[day] = true)
}
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
Victoria
  • 13
  • 6
  • 2
    What type is `slots`? (Obviously it's expected to be an array, but are you sure it actually is at runtime?) – DBS Jan 08 '20 at 10:47
  • Can you show data? – Yosvel Quintero Jan 08 '20 at 10:47
  • I am suspecting that is not array but I'm not sure how to check – Victoria Jan 08 '20 at 10:49
  • 2
    `console.log( { slots, isArray: slots instanceof Array, type: typeof slots });` – Quentin Jan 08 '20 at 10:52
  • Something is missing from this code. You can't have a method definition (`componentWillLoad() { }`) immediately followed by a `for` loop. It's just not valid to have a `for` loop directly inside a class or object definition. You need to provide a [mcve] – Quentin Jan 08 '20 at 10:53
  • Currently betting on this being a duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Jan 08 '20 at 10:54
  • what does `handleDBReponse` do, what is `schedule` and where does it come from (*and what is its initial value before the fetch*) – Gabriele Petrioli Jan 08 '20 at 11:02
  • Surely you meant `if (slots.length && slots.every(slot => slot === true)) { schedule[day] = true; }`. The `(schedule[day] = true)` is a function invocation that fails on a boolean. – Bergi Jan 08 '20 at 11:08

0 Answers0