angular - '未处理的承诺拒绝:','无法读取未定义的属性(读取'then')'
问题描述
我有这个错误:
'Unhandled Promise rejection:', 'Cannot read properties of undefined (reading 'then')', '; Zone:', 'ProxyZone', '; Task:', 'jasmine.onComplete', '; Value:', TypeError: Cannot read properties of undefined (reading 'then')
TypeError: Cannot read properties of undefined (reading 'then')
这是我的.ts
文件:
getPillars = async (): Promise<void> => {
if (localStorage.getItem('pillars')) {
this.pillars = JSON.parse(localStorage.getItem('pillars'));
return;
}
let items = await this.api.send('Categories', 'get', filter ? { filter: filter } : {}).then((res: { data: any[], count: number }) => {
return res.data.map(el => {
return { id: el.id, name: el.name, type: 'Categories' }
});
});
localStorage.setItem('pillars', JSON.stringify(items));
this.pillars = items;
}
还有我的测试文件:
describe('getPillars()', () => {
it('Should validate the session success', () => {
let spy1 = spyOn(apiService, 'send').and.returnValue(Promise.resolve(of('pillars')));
component.getPillars();
expect(spy1).toHaveBeenCalled();
});
});
解决方案
let items = await this.api.send('Categories', 'get', filter ? { filter: filter } : {}).then((res: { data: any[], count: number }) => {
return res.data.map(el => {
return { id: el.id, name: el.name, type: 'Categories' }
});
});
在这里,您等待从发送返回的承诺
但在稍后的测试中,您返回一个解析为另一个承诺的承诺。
您必须使它只是解析为一些实际值
let spy1 = spyOn(apiService, 'send').and.returnValue(Promise.resolve('pillars'));
推荐阅读
- javascript - 如何修复 iOS Safari 上的字体渲染错误
- marklogic - 使用 gradle mlExportToFile 在单个 XML 中导出标题和正文
- javascript - Typescript如何将数组中的子节点移动到父数组?
- javascript - 无法使用 Flask-socketio 和 Javascript 广播消息
- javascript - 在 JSON 文件中创建时间线并根据该 JSON 中的日期时间更新页面
- spatstat - 计算标记变异函数的距离规范
- reactjs - 组件在另一个组件下呈现
- python-3.x - CodingBat Python - 清单 2 总和(67)
- python - 无法将日期转换为所需格式
- python - 在 n 阶导数的定义中,“h”的值应该是多少?