2016-08-09 504 views
11

,我發現了以下錯誤:Possible unhandled promise rejection (id:0: Network request failed陣營原生:可能未處理的承諾拒絕

這裏的承諾的代碼,我看不出有什麼錯在這裏,任何想法?

return fetch(url) 
    .then(function(response){ 
     return response.json(); 
    }) 
    .then(function(json){ 
     return { 
     city: json.name, 
     temperature: kelvinToF(json.main.temp), 
     description: _.capitalize(json.weather[0].description) 
     } 
    }) 
    .catch(function(error) { 
    console.log('There has been a problem with your fetch operation: ' + error.message); 
    }); 
} 

**編輯:我添加了一個捕捉功能,並得到了更好的錯誤You passed an undefined or null state object; instead, use forceUpdate(). index.ios.js:64 undefined

這裏的index.ios.js代碼。網址很好,並給我正確的JSON數據。我可以在控制檯日誌中看到,region.latituderegion.longitudeApi(region.latitude, region.longitude)中可用。但是data未定義。我仍然不確定發生了什麼,爲什麼data有問題,以及爲什麼它沒有定義。

// var React = require('react-native'); --deprecated 

// updated 
import React from 'react'; 

// updated 
import { 
    AppRegistry, 
    MapView, 
    View, 
    Text, 
    StyleSheet, 
} from 'react-native'; 

/* 
var { 
    AppRegistry, 
    MapView, 
    View, 
    Text, 
    StyleSheet 
} = React; 
*/ // -- depreciated 


var Api = require('./src/api'); 

var Weather = React.createClass({ 
    getInitialState: function() { 
    return { 
     pin: { 
     latitude: 0, 
     longitude: 0 
     }, 
     city: '', 
     temperature: '', 
     description: '' 
    }; 
    }, 
    render: function() { 
    return <View style={styles.container}> 
     <MapView 
     annotations={[this.state.pin]} 
     onRegionChangeComplete={this.onRegionChangeComplete} 
     style={styles.map}> 
     </MapView> 
     <View style={styles.textWrapper}> 
     <Text style={styles.text}>{this.state.city}</Text> 
     <Text style={styles.text}>{this.state.temperature}</Text> 
     <Text style={styles.text}>{this.state.description}</Text> 
     </View> 
    </View> 
    }, 
    onRegionChangeComplete: function(region) { 
    this.setState({ 
     pin: { 
     longitude: region.longitude, 
     latitude: region.latitude 
     } 
    }); 

    Api(region.latitude, region.longitude) 
     .then((data) => { 
     console.log(data); 
     this.setState(data); 
     }); 
    } 
}); 




var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'stretch', 
    backgroundColor: '#F5FCFF' 
    }, 
    map: { 
    flex: 2, 
    marginTop: 30 
    }, 
    textWrapper: { 
    flex: 1, 
    alignItems: 'center' 
    }, 
    text: { 
    fontSize: 30 
    } 
}); 

AppRegistry.registerComponent('weather',() => Weather); 
+1

也加上'.catch()',看看錯誤是否消失。參考:https://facebook.github.io/react-native/docs/network.html#handling-the-response – Cherniv

+0

謝謝,這給了我一個更好的錯誤,但沒有擺脫它:DI編輯了上述問題以反映更好的錯誤。 –

回答

14

你的api中的catch函數應該返回一些可以通過React類中的Api調用處理的數據,或者拋出新的錯誤,這應該通過在你的React類中使用catch函數來捕獲碼。後者的做法應該是這樣的:

return fetch(url) 
.then(function(response){ 
    return response.json(); 
}) 
.then(function(json){ 
    return { 
    city: json.name, 
    temperature: kelvinToF(json.main.temp), 
    description: _.capitalize(json.weather[0].description) 
    } 
}) 
.catch(function(error) { 
console.log('There has been a problem with your fetch operation: ' + error.message); 
// ADD THIS THROW error 
    throw error; 
}); 

然後在您的陣營類:

Api(region.latitude, region.longitude) 
    .then((data) => { 
    console.log(data); 
    this.setState(data); 
    }).catch((error)=>{ 
    console.log("Api call error"); 
    alert(error.message); 
    }); 
+0

謝謝,我在api中也加了catch函數,並且沒有拋出錯誤。奇怪。 –

+1

在console.log後加了throw error('有問題你的抓取操作:'+ error.message';在你的抓取函數中,見第一個片段中添加的throw錯誤 – while1

+0

Ah jeez,錯過了,謝謝,錯誤是'網絡請求失敗',這是錯誤嗯...仍然不知道這裏發生了什麼 –

3

您應該將catch()添加到Api調用的結尾處。當你的代碼碰到catch()它不會返回任何東西,所以當你嘗試在它上面使用setState()時,數據是不確定的。錯誤信息實際上也告訴你這個:)

+0

謝謝,我還在api中添加了catch函數,並且不會引發錯誤。奇怪。 –

+0

關於網絡故障錯誤...你是否在模擬器中運行提取?由於本地開發機器上的防火牆,它可能無法訪問網絡?或者,也許android dns無法解析url - 所以試試一個ip地址。 – hasseio

+0

是在ios模擬器中運行它,沒有防火牆。我如何嘗試使用IP地址? –

0

this post,您應該啓用它的XCode。

  1. 點擊在Project Navigator
  2. 打開你的項目的信息選項卡
  3. 點擊向下箭頭留給「應用程序傳輸安全設置」
  4. 右鍵單擊「應用程序傳輸安全設置」並選擇添加行
  5. 對於創建的行,設置鍵「Allow Arbitrary Loads」,鍵入布爾值並將值設置爲YES。

enter image description here

2

這裏添加我的經驗,希望可以幫助別人。

我在使用熱重新加載的Linux中遇到了Android模擬器上的相同問題。代碼是正確的,根據公認的答案和模擬器可以到達互聯網(我需要一個域名)。

手動刷新應用程序使其工作。所以也許它與熱重裝有關。

相關問題