gpt4 book ai didi

javascript - React Native + Fetch => TypeError : Cannot read property 'then' of undefined

转载 作者:行者123 更新时间:2023-12-03 03:01:06 26 4
gpt4 key购买 nike

我使用 CRNA 设置了一个 React Native 项目,并使用 fetch 从这样的 API 返回数据( key 已删除,URL 返回正确的数据,例如在带有我的 api-key 的浏览器中):

export const getNews = () => { 
fetch('https://newsapi.org/v2/sources?apiKey=xxx')
.then(res => res.json())
.then(news => {
console.log(news.sources)
return news.sources
})
}

我已经测试并控制台记录了最后一行的输出,console.log(news.sources) 为我提供了数组上 134 个项目的正确 View ,正如我所期望的那样。

然后,我尝试在组件 componentDidMount 方法中使用 API:

state = { news: '' }

componentDidMount() {
NewsAPI.getNews()
.then(news => {
this.setState({
news
})
})
}

我遇到了标题的错误消息。这是 chrome 中显示的完整错误消息(注意:我发现有点奇怪,我首先收到 .then 错误,但我可以清楚地看到,在错误堆栈的底部,提取是成功并记录了我希望在 componentDidMount() 中处理的输出。)

D:\test\node_modules\react-native\Libraries\Core\ExceptionsManager.js:65 TypeError: Cannot read property 'then' of undefined

This error is located at:
in Channel (at SceneView.js:32)
in SceneView (at CardStack.js:399)
in RCTView (at View.js:113)
in View (at CardStack.js:398)
in RCTView (at View.js:113)
in View (at CardStack.js:397)
in RCTView (at View.js:113)
in View (at createAnimatedComponent.js:134)
in AnimatedComponent (at Card.js:26)
in Card (at PointerEventsContainer.js:55)
in Container (at CardStack.js:440)
in RCTView (at View.js:113)
in View (at CardStack.js:370)
in RCTView (at View.js:113)
in View (at CardStack.js:369)
in CardStack (at CardStackTransitioner.js:103)
in RCTView (at View.js:113)
in View (at Transitioner.js:187)
in Transitioner (at CardStackTransitioner.js:55)
in CardStackTransitioner (at StackNavigator.js:48)
in Unknown (at createNavigator.js:48)
in Navigator (at createNavigationContainer.js:205)
in NavigationContainer (at App.js:8)
in App (created by AwakeInDevApp)
in RCTView (at View.js:113)
in View (created by AwakeInDevApp)
in AwakeInDevApp (at registerRootComponent.js:36)
in RootErrorBoundary (at registerRootComponent.js:35)
in ExpoRootComponent (at renderApplication.js:35)
in RCTView (at View.js:113)
in View (at AppContainer.js:102)
in RCTView (at View.js:113)
in View (at AppContainer.js:126)
in AppContainer (at renderApplication.js:34)
MessageQueue.callFunctionReturnFlushedQueue @ D:\test\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:107
(anonymous) @ debuggerWorker.js:72
D:\test\node_modules\react-native\Libraries\Core\ExceptionsManager.js:65 TypeError: Cannot read property 'then' of undefined

This error is located at:
in Channel (at SceneView.js:32)
in SceneView (at CardStack.js:399)
in RCTView (at View.js:113)
in View (at CardStack.js:398)
in RCTView (at View.js:113)
in View (at CardStack.js:397)
in RCTView (at View.js:113)
in View (at createAnimatedComponent.js:134)
in AnimatedComponent (at Card.js:26)
in Card (at PointerEventsContainer.js:55)
in Container (at CardStack.js:440)
in RCTView (at View.js:113)
in View (at CardStack.js:370)
in RCTView (at View.js:113)
in View (at CardStack.js:369)
in CardStack (at CardStackTransitioner.js:103)
in RCTView (at View.js:113)
in View (at Transitioner.js:187)
in Transitioner (at CardStackTransitioner.js:55)
in CardStackTransitioner (at StackNavigator.js:48)
in Unknown (at createNavigator.js:48)
in Navigator (at createNavigationContainer.js:205)
in NavigationContainer (at App.js:8)
in App (created by AwakeInDevApp)
in RCTView (at View.js:113)
in View (created by AwakeInDevApp)
in AwakeInDevApp (at registerRootComponent.js:36)
in RootErrorBoundary (at registerRootComponent.js:35)
in ExpoRootComponent (at renderApplication.js:35)
in RCTView (at View.js:113)
in View (at AppContainer.js:102)
in RCTView (at View.js:113)
in View (at AppContainer.js:126)
in AppContainer (at renderApplication.js:34)
handleException @ D:\test\node_modules\react-native\Libraries\Core\ExceptionsManager.js:65
handleError @ D:\test\node_modules\react-native\Libraries\Core\InitializeCore.js:106
reportFatalError @ D:\test\node_modules\react-native\Libraries\polyfills\error-guard.js:46
__guard @ D:\test\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:271
MessageQueue.callFunctionReturnFlushedQueue @ D:\test\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:107
(anonymous) @ debuggerWorker.js:72

D:\test\src\utils\NewsAPI.js:14 (134) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}

最佳答案

您在 getNews 函数中获取之前忘记了返回

关于javascript - React Native + Fetch => TypeError : Cannot read property 'then' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47382987/

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