0

I have been trying to figure out that how to insert list of into array and push new data into same array.

collectionList: [any]; 

//Array defined
  collection: [ 
      {
        Name:any,
        Code:any,
        Location:any,
        Capacity:number,
        Remaining:number

      }
  ]
// Getting data from database in collection array to push inside arrayList
this.collection=[{
      Name:item.Name,
      Code:item.Code,
      Location:item.Location,
      Capacity:item.Capacity,
      Remaining:item.Remaining
    }]

//Then Pushing data to collectionList

if (item.register== true){

      this.collectionList.push(this.collection)

    }

MyOrigional code

4
  • 1
    Please consider editing the code above to constitute a true self-contained minimal reproducible example with no external dependencies or missing code (links to external sites don't really meet this need). My guess about this code is that you've defined the type of collectionList without actually initializing it (which would be caught if you compile TS with --strict), so it's undefined at runtime, and you can't call push() on that. Initialize your property (e.g., collectionList: any[] = []) and try again? Commented Nov 17, 2019 at 21:31
  • Yeah initialization was the issue. its working Thanks Commented Nov 17, 2019 at 21:35
  • Possible duplicate of TypeScript: Cannot read property 'push' of undefined in [null] Commented Nov 17, 2019 at 21:38
  • I tried that one. but I was not sure how to initialize the array. And I had doubt that something was missing from my code. Commented Nov 17, 2019 at 21:49

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.