gpt4 book ai didi

javascript - UnhandledPromiseRejectionWarning 而没有则没有 promise

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:06 25 4
gpt4 key购买 nike

最近,我在测试代码时经常遇到此错误。刚才我在尝试执行包含错误的同步代码时再次收到此错误:

(async function() {

const characterProfile = await Model.findOne({'author': author.id}).catch(console.error);

if (!characterProfile) {msg.reply('No characters satisfied your criteria.'); return;}

var displayIndexes = charfields.isEqual("field", name); // <- error "name"

console.log(displayIndexes);

})();

我知道有一个错误,如果它只是告诉我有一个 ReferenceError 就好了,但它说:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: name is not defined

[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.

所以我担心 DeprecationWarning 将来会毁掉我的代码,那将是一场灾难。 charfields.isEqual() 不是一个 promise ,它是一个返回 ids 数组的 forEach 循环。为什么我会收到 promise 错误,就像实际上存在 promise 错误一样?

最佳答案

每次使用 async 关键字时,您都会创建至少 1 个 Promise。异步函数中的所有内容实际上都是 promise 的一部分。

如果您到处使用async,并且您的应用程序有一个调用“main async”函数的入口点,则可以处理如下错误:

async main() {
// Run your app
}

// Call main when your code executes:
main().catch( e => { // do something with 'e' });

关于javascript - UnhandledPromiseRejectionWarning 而没有则没有 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com