Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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
React native 如何找到可能未处理的承诺拒绝的来源_React Native - Fatal编程技术网

React native 如何找到可能未处理的承诺拒绝的来源

React native 如何找到可能未处理的承诺拒绝的来源,react-native,React Native,在处理大型继承的代码库时,我偶然发现了一个不太有用的警告: Possible Unhandled Promise Rejection (id: 0): true console.warn @ YellowBox.js:67 onUnhandled @ Promise.js:43 onUnhandled @ rejection-tracking.js:71 (anonymous) @ JSTimers.js:256 _callTimer @ JSTimers.js:152 callTimers

在处理大型继承的代码库时,我偶然发现了一个不太有用的警告:

Possible Unhandled Promise Rejection (id: 0):
true

console.warn @ YellowBox.js:67
onUnhandled @ Promise.js:43
onUnhandled @ rejection-tracking.js:71
(anonymous) @ JSTimers.js:256
_callTimer @ JSTimers.js:152
callTimers @ JSTimers.js:405
__callFunction @ MessageQueue.js:349
(anonymous) @ MessageQueue.js:106
__guard @ MessageQueue.js:297
callFunctionReturnFlushedQueue @ MessageQueue.js:105
(anonymous) @ debuggerWorker.js:72

是否可以获取导致拒绝的承诺的文件和行号,而无需遍历整个代码并在所有地方添加catch()?

您可以在根组件中执行类似操作

componentDidCatch(error, info) {
    console.log(error)
    console.log(info)
}
不要记录您的错误,而是将其发送到您的数据库,这样您就可以用更多信息修复该问题


解决方案是使用第三方promise库,该库保留额外的堆栈跟踪,如Bluebird,如以下回答所述:

此生命周期方法应该可以帮助您
componentDidCatch
您是使用fetch还是axis获取数据?使用axios,您可以使用拦截器功能并在那里添加更多日志记录。我想,你也可以用fetch来实现这一点。@needsleep应用程序使用fetch,但所有未处理的承诺都会出现问题幸运的是,这不会触发拒绝承诺