【发布时间】:2019-06-05 03:38:02
【问题描述】:
我是 React Native 和一般数据库的新手。我目前正在尝试创建一个类来获取用户的当前位置(纬度和经度)并将其转换为谷歌地图的 url 以将其存储在数据库中。但是,每当我尝试编译我的代码时,我都会收到错误消息。它还向我显示了 storeData() 的错误
SyntaxError: C:\Users\xymlt\OneDrive\Desktop\not-arbit\components\Geolocation.js: Missing catch or finally clause (57:3)
55 |
56 | storeData = async () => {
> 57 | try {
| ^
58 | await AsyncStorage.setItem
59 | }
60 | }
我尝试搜索有关如何显示数据,尤其是如何使用 try catch 块的 AsyncStorage 示例。我正在查看https://github.com/react-native-community/react-native-async-storage/blob/master/docs/API.md#getAllKeys 中的示例,但它没有提供任何关于 try catch 块应该是什么样子的具体示例。请帮忙。
import React from 'react';
import {View, Text, StyleSheet, Image , PermissionsAndroid, Platform} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
export default class Carpool extends React.Component {
state = { // set current state
currentLongitude: null, //Initial Longitude
currentLatitude: null, //Initial Latitude
}
componentDidMount = () => {
var that = this;
//checl for system permissions
if(Platform.OS === 'ios'){
this.callLocation(that);
}else{
async function requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,{
'title': 'Location Access Required',
'message': 'This app needs to access your location'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
that.callLocation(that);
} else {
alert("Permission Denied");
}
} catch (err) {
alert("err",err);
console.warn(err)
}
}
requestLocationPermission();
}
}
callLocation(that){ //function gives current location
navigator.geolocation.getCurrentPosition(
(position) => {
const currentLongitude = JSON.stringify(position.coords.longitude); //getting the Longitude from the location json
const currentLatitude = JSON.stringify(position.coords.latitude); //getting the Latitude from the location json
this.storeData(currentLatitude, currentLongitude);
that.setState({ currentLongitude:currentLongitude }); //Setting state Longitude to re re-render the Longitude Text
that.setState({ currentLatitude:currentLatitude }); //Setting state Latitude to re re-render the Longitude Text
},
(error) => alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
}
componentWillUnmount = () => {
navigator.geolocation.clearWatch(this.watchID);
}
storeData = async (lat, long) => { //return a link instead
var url = 'geo:0,0?q=' + 'lat,long';
try {
await AsyncStorage.setItem('urlOne', JSON.stringify(url));
} catch (error) {
alert("error",error);
console.warn(error)
}
}
getData = async(key) => {
try {
const value = await AsyncStorage.getItem(key)
if(value !== null) {
return(JSON.parse(url));
}
} catch(error) {
alert("error",error);
console.warn(error)
}
}
render() {
return (
<View style = {styles.container}>
<Text style = {styles.boldText}>
You are Here
</Text>
<Text style={{justifyContent:'center',alignItems: 'center',marginTop:16}}>
Longitude: {this.state.currentLongitude}
</Text>
<Text style={{justifyContent:'center',alignItems: 'center',marginTop:16}}>
Latitude: {this.state.currentLatitude}
</Text>
{this.getData('url')}
</View>
)
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: 'center',
justifyContent:'center',
marginTop: 50,
padding:16,
backgroundColor:'white'
},
boldText: {
fontSize: 30,
color: 'red',
}
})
【问题讨论】:
-
您发布的示例确实有一个示例。查看 getData 函数。 catch 块只是警告并记录错误。我建议不要使用警报...这听起来很烦人。
-
或者您是在问一般如何进行 try catch?
-
@ChrisRollins 一般如何使用它以及为什么它向我显示该错误。
-
除了其他人的建议之外,您可能还需要更正
line 58以指定要保存到 AsyncStorage 的属性及其值。在线查看适合初学者的教程,例如here's one to get you started off
标签: javascript react-native expo