I have a material design table and I wrote custom functions to load the data and extract the objects from the JSON array object.
I have the following code:
public getDocumentList() {
return this.http.get(this.getDocumentUrl, this.httpOptions)
.subscribe(
documentResponse => {
for (let i = 0; i > Object.keys(documentResponse).length; i++){
console.log(Object.keys(documentResponse));
console.log("documentResponse:");
console.log(documentResponse);
console.log(of(documentResponse[i]).pipe(pluck('soap:Envelope', 'soap:Body', 'ns2:getDocumentsResponse', 'return')));
this.documentList$ = of(documentResponse[i]).pipe(pluck('soap:Envelope', 'soap:Body', 'ns2:getDocumentsResponse', 'return'));
this.documentList$.subscribe(x => this.documentListArray.push(x));
console.log("Doklist", this.documentListArray)
}
this.setDokumentStatus();
},
error => {
alert('Following error happened:' + ' ' + error['statusText']);
console.log('There was an error: ', error);
});
}
The following public function above fills documentListArray
with the required objects...
But this semi-random error gets thrown:
ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')
at WorkDocumentComponent.<anonymous> (work-document.component.ts:139:6
The last line of this code is where the error happens (before the bracket obviously):
async FillElementDataArray() {
ELEMENT_DATA.length = 0;
this.dataSource.connect().next(ELEMENT_DATA);
let add_WorkDocument = {} as WorkDocument;
console.log(this.DataService.documentListArray);
let docsForMetadata;
// this below is undefined yo
for (let i = 0; i < this.DataService.documentListArray[0].length; i++)
{
...which is VERY strange.
Is this an async and sync function issue?
- How do I fix this?
- Why is it still undefined and I cannot console.log it? EDIT: I can console.log it IF I put the console.log before the log loop.
P.S. my documentResponse looks like this:
[
{
"soap:Envelope": {
"soap:Body": {
"ns2:getDocumentMetaDataResponse": {
"return": {
"items": [
{
"key": "blah",
"values": "blablabla"
and so on...
}
]