Javascript 反应本机+;获取=>;TypeError:无法读取属性';然后';未定义的
我使用CRNA设置了一个React本机项目,并使用fetch从API返回数据,如下所示(密钥已删除,url返回正确的数据,例如在浏览器中使用我的API密钥): 我已经测试并记录了最后一行的输出,console.log(news.sources)为我提供了一个数组中134项的正确视图,正如我所期望的那样 然后,我尝试在我的componentsJavascript 反应本机+;获取=>;TypeError:无法读取属性';然后';未定义的,javascript,react-native,fetch,Javascript,React Native,Fetch,我使用CRNA设置了一个React本机项目,并使用fetch从API返回数据,如下所示(密钥已删除,url返回正确的数据,例如在浏览器中使用我的API密钥): 我已经测试并记录了最后一行的输出,console.log(news.sources)为我提供了一个数组中134项的正确视图,正如我所期望的那样 然后,我尝试在我的componentscomponentDidMount方法中使用API: state = { news: '' } componentDidMount() { NewsAP
componentDidMount
方法中使用API:
state = { news: '' }
componentDidMount() {
NewsAPI.getNews()
.then(news => {
this.setState({
news
})
})
}
在这里,我看到了标题的错误消息。以下是chrome中显示的完整错误消息(注意:我发现这有点奇怪,我首先得到。然后是错误,但我可以清楚地看到,在错误堆栈的底部,获取成功,它记录了我希望在componentDidMount()中处理的输出。)
您在获取getNews函数之前忘记了返回谢谢!很容易对自己的工作视而不见。。实际上只是删除了隐含返回的花括号
state = { news: '' }
componentDidMount() {
NewsAPI.getNews()
.then(news => {
this.setState({
news
})
})
}
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) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}