Javascript 可能未处理的承诺拒绝(id:0)警告
当我的AsyncStorage项为空时,我收到以下警告消息“可能未处理的承诺拒绝(id:0)”,因此我的问题是:如何处理承诺拒绝 我的代码:Javascript 可能未处理的承诺拒绝(id:0)警告,javascript,react-native,promise,asyncstorage,Javascript,React Native,Promise,Asyncstorage,当我的AsyncStorage项为空时,我收到以下警告消息“可能未处理的承诺拒绝(id:0)”,因此我的问题是:如何处理承诺拒绝 我的代码: componentDidMount() { try { // This warning only appears when 'connections' item is empty AsyncStorage.getItem('connections').then((token) => {
componentDidMount() {
try {
// This warning only appears when 'connections' item is empty
AsyncStorage.getItem('connections').then((token) => {
token = JSON.parse(token);
const getSectionData = (dataBlob, sectionId) => dataBlob[sectionId];
const getRowData = (dataBlob, sectionId, rowId) => dataBlob[`${rowId}`];
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
getSectionData,
getRowData,
});
const {dataBlob, sectionIds, rowIds} = this.formatData(token);
this.setState({
dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIds, rowIds),
});
});
}catch(error) {
console.log(error);
}
}
你需要抓住承诺的拒绝:
componentDidMount() {
// This warning only appears when 'connections' item is empty
return AsyncStorage.getItem('connections').then((token) => {
token = JSON.parse(token);
const getSectionData = (dataBlob, sectionId) => dataBlob[sectionId];
const getRowData = (dataBlob, sectionId, rowId) => dataBlob[`${rowId}`];
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
getSectionData,
getRowData,
});
const { dataBlob, sectionIds, rowIds } = this.formatData(token);
this.setState({
dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIds, rowIds),
});
}).catch(error => {
console.log(error);
})
}
Axios将不允许从http URL请求数据。 有一些框架可以让您的生活变得轻松,并允许您从不安全的http获取数据(http://something). 通常这些框架允许在开发模式下而不是在发布模式下使用 例如Expo cli将允许在android和ios的开发模式下从非安全http获取数据。 React native CLI不允许从不安全的http获取数据。因此,有一些解决方法,通过这些方法,您将能够从非安全http获取react native cli中的数据 ios示例 打开-info.plist文件(ios/projectname/info.plist) 在NSAppTransportSecurity下添加以下内容
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
...
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSExceptionDomains
...
我在“=>”上得到一个错误,当我像这样实现它时.catch(console.log('empty'))会打印出来,但一直给我相同的警告消息try
/catch
除非您使用wait
,否则对承诺无效。