useEffect(() => {
(async () => {
await httpClient
.get(`${config.resourceServerUrl}/inventory/`)
.then((response) => {
setSponsor(response.data.sponsor);
setAddress(response.data.address);
setPhaseOfTrial(response.data.phaseOfTrial);
.catch((error) => {
alert(error);
})();
}, []);
此代码成功运行。唯一的问题是,它在开玩笑的测试用例中失败了。
describe('ListInventory', () => {
it('should render successfully', () => {
const { baseElement } = render(<ListInventory />);
expect(baseElement).toBeTruthy();
下面是错误。
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: Cannot read properties of undefined (reading 'get')".] {
code: 'ERR_UNHANDLED_REJECTION'
我已经在使用 catch
块。代码有什么问题?你能帮我做同样的事吗?
原文由 Shruti sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议