我有一个材料设计表,并编写了自定义函数来加载数据并从 JSON 数组对象中提取对象。
我有以下代码:
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);
});
}
上面的公共函数如下documentListArray
与所需的对象...
但是会抛出这个半随机错误:
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
这段代码的最后一行是错误发生的地方(显然在括号之前):
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++)
{
...这很奇怪。
这是异步和同步功能问题吗?
- 我该如何解决?
- 为什么它仍然未定义并且我无法 console.log 它?编辑:如果我将 console.log 放在日志循环之前,我可以 console.log 。
附:我的文档响应如下所示:
[
{
"soap:Envelope": {
"soap:Body": {
"ns2:getDocumentMetaDataResponse": {
"return": {
"items": [
{
"key": "blah",
"values": "blablabla"
and so on...
}
]