React Native + Fetch => TypeError: 无法读取未定义的属性'then'



i使用crna设置了一个反应本机项目,我正在使用fetch从API返回数据(删除键,URL返回正确的数据):

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

我已经测试和控制台记录了最后一行和控制台的输出。

然后,我尝试在组件中消耗API componentDidMount方法:

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

我遇到标题的错误消息的位置。这是Chrome中显示的完整错误消息(注意:我觉得有点奇怪,我首先会收到.then错误,但我可以清楚地看到,在错误堆栈的底部,获取成功,并且记录了我的输出期望在ComponentDidmount())

中处理
D:testnode_modulesreact-nativeLibrariesCoreExceptionsManager.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:testnode_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:107
(anonymous) @ debuggerWorker.js:72
D:testnode_modulesreact-nativeLibrariesCoreExceptionsManager.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:testnode_modulesreact-nativeLibrariesCoreExceptionsManager.js:65
handleError @ D:testnode_modulesreact-nativeLibrariesCoreInitializeCore.js:106
reportFatalError @ D:testnode_modulesreact-nativeLibrariespolyfillserror-guard.js:46
__guard @ D:testnode_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:271
MessageQueue.callFunctionReturnFlushedQueue @ D:testnode_modulesreact-nativeLibrariesBatchedBridgeMessageQueue.js:107
(anonymous) @ debuggerWorker.js:72
D:testsrcutilsNewsAPI.js:14 (134) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}

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

相关内容

  • 没有找到相关文章