Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 环回异步/等待未处理PromiserEjectionWarning问题_Node.js_Asynchronous_Async Await_Loopbackjs_Loopback - Fatal编程技术网

Node.js 环回异步/等待未处理PromiserEjectionWarning问题

Node.js 环回异步/等待未处理PromiserEjectionWarning问题,node.js,asynchronous,async-await,loopbackjs,loopback,Node.js,Asynchronous,Async Await,Loopbackjs,Loopback,在环回3中,当我在保存前操作钩子中实现异步/等待时 Entry.observe('before save', async (ctx, next) =>{ if(ctx.instance.images){ Entry.upload(ctx.instance.images[0].src).then(data => { ctx.instance.image = data; }); } }); 控制台将打印此错误 (

在环回3中,当我在保存前操作钩子中实现异步/等待时

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.我不知道该承诺在哪里,一旦我删除async/await,该消息就会消失。

您需要为条目指定.catch.upload承诺

Entry.upload(ctx.instance.images[0].src).then(data => {
        ctx.instance.image = data;
    }).catch(console.error);

一些旧版本的NodeJS可能不会出现这种错误。您需要始终使用catch for Promissions,否则应用程序将崩溃。

使用
try/catch
阻止并始终在声明
异步
函数(或Promise函数)时返回已解决或拒绝的承诺


同样的问题,我也删除了该块,它与
async
有关,当我删除它时,错误就消失了…@dyaa在该异步函数中进行了一次尝试,就在
之前,如果(ctx.instance.images)
,似乎错误就在某个地方,ctx.instance定义了吗?
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()
});