.then 的结果在 .then 之后未定义
Result of .then is undefined after .then
我正在加载一个 .json 文件,将其保存到一个数组中并稍微编辑我的数组。不幸的是,我是 angular 的新手,我不知道如何在 Ionic4(Angular 7.2.2) 中处理异步请求(之前仅在 php 和客户端 js 中开发过)。
我的 json 文件的加载按预期进行。但是,如果我想通过循环编辑文件,我想我的承诺会在循环完成之前得到解决。这是我的代码。显示输出的注释。我究竟做错了什么?你能推荐一些东西来更好地学习 angular 中的 async/sync 请求吗?
public getCategories()
{
let arrCategories;
return new Promise((resolve, reject) =>
{
this.getCategoriesData().subscribe(arrResult =>
{
console.log('Ln 32');
console.log(JSON.stringify(arrResult)); // Expected array
this.setAbkuerzungenDEAndBild(arrResult).then(arrKategorien =>
{
console.log('Ln 35');
console.log(JSON.stringify(arrKategorien)); // Output: []
resolve(arrKategorien);
},
error =>
{
reject('Failed loading Categories. [1]');
}
);
},
error =>
{
reject('Failed loading Categories. [2]');
}
);
});
}
private getCategoriesData() //Working as expected
{
const strUrl = 'assets/data/Kategorien.json';
return this.objHttp.get(strUrl).pipe(
map((res: Response) => res),
catchError(CategoriesService.handleError)
);
}
private setAbkuerzungenDEAndBild(arrCategories) //Returning [] after .then and later on the array as expected
{
let arrNewCategories = [];
return new Promise((resolve, reject) =>
{
this.objStorage.get('objSprache').then(async objSprache =>
{
await arrCategories.forEach((objKategorie, intIndex) =>
{
if (objKategorie.intFKSpracheID === objSprache.intSpracheID) //Wenn Sprache == ausgewählt
{
//set something
if (objKategorie.intFKSpracheID !== '1') //Sprache nicht de
{
for (let objKategorieDE of arrCategories)
{
//Set something
}
}
}
});
});
resolve(arrNewCategories);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
我已使用以下解决方法自行修复了错误:
我用
替换了我的 resolve()
if(intIndex == arrCategories.length-1)
{
resolve(arrNewCategories);
}
我正在加载一个 .json 文件,将其保存到一个数组中并稍微编辑我的数组。不幸的是,我是 angular 的新手,我不知道如何在 Ionic4(Angular 7.2.2) 中处理异步请求(之前仅在 php 和客户端 js 中开发过)。
我的 json 文件的加载按预期进行。但是,如果我想通过循环编辑文件,我想我的承诺会在循环完成之前得到解决。这是我的代码。显示输出的注释。我究竟做错了什么?你能推荐一些东西来更好地学习 angular 中的 async/sync 请求吗?
public getCategories()
{
let arrCategories;
return new Promise((resolve, reject) =>
{
this.getCategoriesData().subscribe(arrResult =>
{
console.log('Ln 32');
console.log(JSON.stringify(arrResult)); // Expected array
this.setAbkuerzungenDEAndBild(arrResult).then(arrKategorien =>
{
console.log('Ln 35');
console.log(JSON.stringify(arrKategorien)); // Output: []
resolve(arrKategorien);
},
error =>
{
reject('Failed loading Categories. [1]');
}
);
},
error =>
{
reject('Failed loading Categories. [2]');
}
);
});
}
private getCategoriesData() //Working as expected
{
const strUrl = 'assets/data/Kategorien.json';
return this.objHttp.get(strUrl).pipe(
map((res: Response) => res),
catchError(CategoriesService.handleError)
);
}
private setAbkuerzungenDEAndBild(arrCategories) //Returning [] after .then and later on the array as expected
{
let arrNewCategories = [];
return new Promise((resolve, reject) =>
{
this.objStorage.get('objSprache').then(async objSprache =>
{
await arrCategories.forEach((objKategorie, intIndex) =>
{
if (objKategorie.intFKSpracheID === objSprache.intSpracheID) //Wenn Sprache == ausgewählt
{
//set something
if (objKategorie.intFKSpracheID !== '1') //Sprache nicht de
{
for (let objKategorieDE of arrCategories)
{
//Set something
}
}
}
});
});
resolve(arrNewCategories);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
我已使用以下解决方法自行修复了错误: 我用
替换了我的 resolve() if(intIndex == arrCategories.length-1)
{
resolve(arrNewCategories);
}