'Unhandled Promise rejection:'、'无法读取未定义的属性(读取 'then')'

'Unhandled Promise rejection:', 'Cannot read properties of undefined (reading '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' }
            });
        });

在这里你等待一个承诺return从发送

但稍后在测试中 return 一个承诺会分解为另一个承诺。

你必须让它解析成一些实际值

let spy1 = spyOn(apiService, 'send').and.returnValue(Promise.resolve('pillars'));

对于有类似问题但未被接受的答案解决的人: 我错误地导入了库。检查您正在使用的库,看看他们建议您如何导入它。

import openpgp from 'openpgp';
// Throws unhandled promise rejection.
const {notImportant} = await openpgp.readKey({...}); 
import * as openpgp from 'openpgp';

// Executes successfully.
const {notImportant} = await openpgp.readKey({...});