1

I have tried various examples but none has worked so far. I have a GET response as follows:

{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …} config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}


data:

sales: Array(2) 0: {Id: 2, Amount: 500, DateCreated: "01/01/2019 00:00:00"} 1: {Id: 3, Amount: 410, DateCreated: "01/20/2019 00:00:00"}

I have an interface definition as follows:

interface ISales{
  id:number;
  amount:number;
  dateCreated:Date
}

I want to create an array and fill in the values from the response sales. I have tried the following:

 const result: ISales = response.data.sales;
      let payments: ISales[];
      payments = [];
      payments.push(result);


payments.map((item)=>{"whole array", console.log(item)})

payments.map((item)=>{"property only", console.log(item.amount)})

In the above the whole array displays the data right as

(2) [{…}, {…}] 0: {Id: 2, Amount: 500, DateCreated: "01/01/2019 00:00:00"} 1: {Id: 3, Amount: 410, DateCreated: "01/20/2019 00:00:00"} length: 2 proto: Array(0)

Whereas the property only is undefined. What is wrong with my code. How can I read the property value and store it in payments?

1
  • payment is empty, set it ->payments = result; Commented Jun 25, 2019 at 11:07

2 Answers 2

1

I finally managed to find the error. The properties in the interface were camel case whereas, those from the response were pascal case.

Sign up to request clarification or add additional context in comments.

Comments

0

This should work:

const result: ISales[] = response.data.sales;
result.map((isales: ISales)=>{console.log(isales)});
result.map((isales: ISales)=>{console.log(isales.amount)});

1 Comment

It gives the errorerror: TypeError: Cannot read property 'map' of undefined at localhost:3000/static/js/bundle.js:67569:24 at step (localhost:3000/static/js/bundle.js:63911:23) at Object.next message: "Cannot read property 'map' of undefined" stack: "TypeError: Cannot read property 'map' of undefined↵ at proto: Error

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.