以下是我在graphql(阿波罗服务器)服务器中的onDisconnect函数的代码(但不是特定于graphql的)。它包含一个postgres事务,该事务通过DB
适配器使用。该代码可以工作,但是只要等待发生错误,我都会不断收到警告。以下是我的代码以及警告。我是异步/等待的新手,不确定自己做错了什么。
onDisconnect: () => {
try {
DB.tx(async t => {
const do_something = await t.any(`SELECT *
FROM something`, []).catch((e) => { throw `error deleting socket` })
... more awaits here ...
console.log(do_something)
}
})
} catch (error) {
console.log(error)
}
},
(node:5640) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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().
(rejection id: 3)
(node:5640) [DEP0018] 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.
我还尝试了以下方法:
onDisconnect: () => {
try {
return DB.tx(async t => {
const do_something = await t.any(`SELECT *
FROM something`, []).catch((e) => { throw `error deleting socket` })
... more awaits here ...
console.log(do_something)
return {
success: 1
}
}
})
} catch (error) {
console.log(error)
throw error
}
},
导出为其他情况下的功能无需警告即可使用,例如:
export function do_another_thing(...) {
try {
return DB.tx(async t => {
const do_something = await t.any(`SELECT *
FROM nothing`, []).catch((e) => { throw `error fetching data` })
... more awaits here ...
console.log(do_something)
return {
success: 1
}
}
})
} catch (error) {
console.log(error)
throw error
}
},
答案 0 :(得分:0)
您必须直接catch
个await
:
export function do_another_thing(...) {
return DB.tx(async t => {
try {
const do_something = await t.any(`SELECT * FROM nothing`, []).catch((e) => { throw `error fetching data` })
//.. more awaits here ...
console.log(do_something)
return {
success: 1
};
} catch(error) {
console.log(error);
// handle the error properly!
}
});
}
提示:重新投掷没有处理...