回环asynchronous/等待UnhandledPromiseRejectionWarning问题
在回环3当我实现asynchronous/等待在前保存操作挂钩
Entry.observe('before save', async (ctx, next) =>{ if(ctx.instance.images){ Entry.upload(ctx.instance.images[0].src).then(data => { ctx.instance.image = data; }); } });
控制台打印这个错误
(node:29323) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Callback was already called. (node:29323) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我怎样才能摆脱这个问题!?
PS。 我不知道这个承诺在哪里,这个消息一旦消除asynchronous/等待消失。
您需要为Entry.upload承诺指定.catch。
Entry.upload(ctx.instance.images[0].src).then(data => { ctx.instance.image = data; }).catch(console.error);
一些旧版本的NodeJS可能不会有这个错误。 您需要始终使用catch来承诺,否则应用程序将崩溃。
使用try/catch
块并总是返回一个Promiseparsing或拒绝时声明async
函数(或Promise函数)
Entry.observe('before save', async (ctx) =>{ try { ctx.instance.image = await Entry.upload(ctx.instance.images[0].src) } catch(error){ return Promise.reject(error) } return Promise.resolve() });