0

I have an Angular app and I am getting the contents of a json file from Google Drive. The contents is an array of 1011 customers that I want to transform into an object. However the reduce function always returns an empty object. Inside the function, when I check for a value of the key I want to use, the debugger says it is undefined, although it is not when I log the object to the console. Can you identify what is going wrong?

enter image description here

  private getCustomers() {
    return this.httpSrv.getFileContent(this.customersFileId).then((res) 
      => {
      return res.result.reduce((acc: any, cur: Customer) => {
        acc[cur.systemNumber] = cur;
        return acc;
      }, {});
    });

After the above code runs, I get one object with one property, that is undefined and its value is the last customer in the collection }

5
  • 1
    since you are having an async function debugger might have issues, instead do console.log and check the final output, if it's fine, then proceed with ignoring this Commented Jun 2, 2024 at 7:15
  • Can you have your code in textual form? Commented Jun 2, 2024 at 8:43
  • i removed async/await to use promises, still the same result Commented Jun 2, 2024 at 11:50
  • Where does the systemNumber come from? Are you sure it is part of the file/json data? Commented Jun 2, 2024 at 14:13
  • The code you have in the image is not fully visible, and is different from the text-version of the code below the image. Please provide the actual code as text, using propper indentation. Commented Jun 2, 2024 at 17:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.