0

I'm getting subject error in my angular app when looping through json data and trying to assign that data to an array of a data type class. my code is:

.. 
import {UnMarkedData} from './mt-UnMarkedData';
...
this.httpClient.get(GlobalConstants.DigAlertServiceURL + "TicketUnMarkedGet" )
.subscribe((data: any) => { 
 //let ticket: string = data.UnMarked[0].Ticket;//this assignment works
 var ticketData: UnMarkedData[];
 for (let i=0;i<data.UnMarked.length; i++){
    ticketData[i].ID =  data.UnMarked[i].ID;//can't set property of undefined occurs here
    ticketData[i].Ticket = data.UnMarked[i].Ticket;
    ticketData[i].DateRcvd = this.FormatDate(data.UnMarked[i].DateRcvd);
 }

The UnMarkedData class is:

export class UnMarkedData {
    public ID: number; 
    public Ticket: string; 
    public DateRcvd: string;
constructor() { 

}

}

ideas of where I'm going wrong here?

Thanks

Pete

I thought one of these would work, but still getting same error:

var ticketDatas:UnMarkedData [] = [];
 //var ticketData: [{ID:number, Ticket:string, DateRcvd:string}];//gives error
 for (let i=0;i<data.UnMarked.length; i++){
   //let ticketData:UnMarkedData = new UnMarkedData();//get error
   let ticketData:UnMarkedData = {ID:undefined, Ticket:undefined, DateRcvd:undefined};//get error
    ticketData[i].ID =  data.UnMarked[i].ID;
    ticketData[i].Ticket = data.UnMarked[i].Ticket;
    ticketData[i].DateRcvd = this.FormatDate(data.UnMarked[i].DateRcvd);
    ticketDatas.push(ticketData);
 }
1
  • 2
    You typed ticketData as and array of UnMarkedData , but you didn't initialize it. Commented Feb 11, 2021 at 1:18

2 Answers 2

2

As Randy pointed in a comment, you have not initialised the array. So that means at runtime, you're attempting to access an index of undefined;

var ticketData: UnMarkedData[] = []; // <-- Initialise the array as an empty list
for (let i=0;i<data.UnMarked.length; i++){
    ticketData[i].ID =  data.UnMarked[i].ID;
    ticketData[i].Ticket = data.UnMarked[i].Ticket;
    ticketData[i].DateRcvd = this.FormatDate(data.UnMarked[i].DateRcvd);
}
Sign up to request clarification or add additional context in comments.

3 Comments

implemented your suggestion and still getting same error -- do I need to intitialize the UnMarkedData class as new?
made some edits at bottom of my post showing two new ways to initialize, but cant get it working
Please refer to the post by @pvitt for working code and in future remember that : SomeType just denotes a type in TypeScript, you still have to initialise a value.
0

this worked, just needed to look at it more closely: Thanks Randy and El-Mo, your ideas were right

  var ticketDatas:UnMarkedData [] = [];

 for (let i=0;i<data.UnMarked.length; i++){
   let ticketData:UnMarkedData = new UnMarkedData();
    ticketData.ID =  data.UnMarked[i].ID;//was mistakenly assigning ticketData[i].ID
    ticketData.Ticket = data.UnMarked[i].Ticket;
    ticketData.DateRcvd = this.FormatDate(data.UnMarked[i].DateRcvd);
    ticketDatas.push(ticketData);
 }

Comments

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.