Javascript 然后让他们登录 //res:结果 如果(res.sucess==真){ AysncStorage.setItem('user',res.user); this.props.navigation.navigate('Profile');//将用户导航到配置文件页面 } //否则,告诉用户它们在数据库中不存在 否则{ 警报(res.message); } }) .完成(); } } const styles=StyleSheet.create({ 包装器:{ 弹性:1, }, 容器:{ 弹性:1, 背景颜色:“#2896d3”, 对齐项目:“居中”, 为内容辩护:“中心”, paddingLeft:40, paddingRight:40, }, 标题:{ 尺寸:24, marginBottom:60, 颜色:“#fff”, 为内容辩护:“中心”, paddingLeft:40, paddingRight:40, }, 文本输入:{ 自我定位:“拉伸”, paddingLeft:16, marginBottom:20, 背景颜色:“#fff”, }, btn:{ 自我定位:“拉伸”, 填充:20, marginBottom:20, 背景颜色:“#01c853”, 对齐项目:“居中”, }, });
在Javascript 然后让他们登录 //res:结果 如果(res.sucess==真){ AysncStorage.setItem('user',res.user); this.props.navigation.navigate('Profile');//将用户导航到配置文件页面 } //否则,告诉用户它们在数据库中不存在 否则{ 警报(res.message); } }) .完成(); } } const styles=StyleSheet.create({ 包装器:{ 弹性:1, }, 容器:{ 弹性:1, 背景颜色:“#2896d3”, 对齐项目:“居中”, 为内容辩护:“中心”, paddingLeft:40, paddingRight:40, }, 标题:{ 尺寸:24, marginBottom:60, 颜色:“#fff”, 为内容辩护:“中心”, paddingLeft:40, paddingRight:40, }, 文本输入:{ 自我定位:“拉伸”, paddingLeft:16, marginBottom:20, 背景颜色:“#fff”, }, btn:{ 自我定位:“拉伸”, 填充:20, marginBottom:20, 背景颜色:“#01c853”, 对齐项目:“居中”, }, });,javascript,react-native,Javascript,React Native,在组件将装入方法之前,您有一个额外的右括号。再粘贴一些代码,这样我们就更容易提供帮助。正如你所知道的(和我们一样),这两行代码中没有任何错误。这是令人惊讶的事情。我甚至无法输入这两行代码而不出错。我会发布完整的代码。感谢我所看到的,componentWillMount似乎不在导出范围内(您在构造函数中用1个括号结束) componentWillMount(){ } import React from 'react'; import { StyleSheet, Text, View,Navig
组件将装入
方法之前,您有一个额外的右括号。再粘贴一些代码,这样我们就更容易提供帮助。正如你所知道的(和我们一样),这两行代码中没有任何错误。这是令人惊讶的事情。我甚至无法输入这两行代码而不出错。我会发布完整的代码。感谢我所看到的,componentWillMount似乎不在导出范围内(您在构造函数中用1个括号结束)
componentWillMount(){
}
import React from 'react';
import { StyleSheet, Text, View,Navigator,TextInput, KeyboardAvoidingView,TouchableOpacity,
AsyncStorage,
} from 'react-native';
import Banana from './Banana';
//var mongoose = require('mongoose'); I cant use mongoose because the front end doesn't have Node.js
import api from './utilities/server_connect'
import {
StackNavigator,
} from 'react-navigation';
export default class Login extends React.Component {
//check to see if user has logged in already
componentDidMount(){
this._loadInitialState().done();
}
//get info from async storage
_loadInitialState = async () => {
var value = await AsyncStorage.getItem('user');
if(value != null){ //if the user is already logged in
this.props.navigation.navigate('Profile'); //**profile page that we will create later
}
}
constructor(props){
super(props);
this.state = {
username: '',
password: '',
list: [], //starting with empty array so its allocated before the fetch method works
}
}
//cwm is a lifecycle method
//its a method that fires right before render happens
}
componentWillMount(){
api.getData().then((res) => {
this.setState({
list: res.data,
})
});
}
render() {
return (
//<View style={styles.container}>
<KeyboardAvoidingView behavior = 'padding' style = {styles.wrapper}>
<View style = {styles.container}>
<Text style={styles.header}> - LOGIN - {this.state.data} </Text>
<TextInput
style={styles.textInput} placeholder='Username'
onChangeText={(username) => this.setState({username})}
/>
<TextInput
style={styles.textInput} placeholder='Password'
onChangeText={(password) => this.setState({password})}
/>
</View>
<TouchableOpacity
style={styles.btn}
onPress = {this.login}>
<Text>Log in</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
// </View>
);
}
login = () => {
alert('test');
//send to server
fetch('http://1.1.1.1/3000/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
//handle response
.then((response) => response.json())
.then((res) => {
//if user and pass exists, then log them in
// res: result
if(res.sucess === true){
AysncStorage.setItem('user',res.user);
this.props.navigation.navigate('Profile'); //navigate user to profile page
}
//else, tell the user they dont exist in the database
else{
alert(res.message);
}
})
.done();
}
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: '#2896d3',
alignItems: 'center',
justifyContent: 'center',
paddingLeft: 40,
paddingRight: 40,
},
header: {
fontSize: 24,
marginBottom: 60,
color: '#fff',
justifyContent: 'center',
paddingLeft: 40,
paddingRight: 40,
},
textInput: {
alignSelf: 'stretch',
paddingLeft: 16,
marginBottom: 20,
backgroundColor: '#fff',
},
btn: {
alignSelf: 'stretch',
padding: 20,
marginBottom: 20,
backgroundColor: '#01c853',
alignItems: 'center',
},
});
import React from 'react';
import { StyleSheet, Text, View,Navigator,TextInput, KeyboardAvoidingView,TouchableOpacity,
AsyncStorage,
} from 'react-native';
import Banana from './Banana';
//var mongoose = require('mongoose'); I cant use mongoose because the front end doesn't have Node.js
import api from './utilities/server_connect'
import {
StackNavigator,
} from 'react-navigation';
export default class Login extends React.Component {
//check to see if user has logged in already
componentDidMount(){
this._loadInitialState().done();
}
//get info from async storage
_loadInitialState = async () => {
var value = await AsyncStorage.getItem('user');
if(value != null){ //if the user is already logged in
this.props.navigation.navigate('Profile'); //**profile page that we will create later
}
}
constructor(props){
super(props);
this.state = {
username: '',
password: '',
list: [], //starting with empty array so its allocated before the fetch method works
}
}
//cwm is a lifecycle method
//its a method that fires right before render happens
}
componentWillMount(){
}
render() {
return (
//<View style={styles.container}>
<KeyboardAvoidingView behavior = 'padding' style = {styles.wrapper}>
<View style = {styles.container}>
<Text style={styles.header}> - LOGIN - {this.state.data} </Text>
<TextInput
style={styles.textInput} placeholder='Username'
onChangeText={(username) => this.setState({username})}
/>
<TextInput
style={styles.textInput} placeholder='Password'
onChangeText={(password) => this.setState({password})}
/>
</View>
<TouchableOpacity
style={styles.btn}
onPress = {this.login}>
<Text>Log in</Text>
</TouchableOpacity>
</KeyboardAvoidingView>
// </View>
);
}
login = () => {
alert('test');
//send to server
fetch('http://1.1.1.1/3000/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
//handle response
.then((response) => response.json())
.then((res) => {
//if user and pass exists, then log them in
// res: result
if(res.sucess === true){
AysncStorage.setItem('user',res.user);
this.props.navigation.navigate('Profile'); //navigate user to profile page
}
//else, tell the user they dont exist in the database
else{
alert(res.message);
}
})
.done();
}
}
const styles = StyleSheet.create({
wrapper: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: '#2896d3',
alignItems: 'center',
justifyContent: 'center',
paddingLeft: 40,
paddingRight: 40,
},
header: {
fontSize: 24,
marginBottom: 60,
color: '#fff',
justifyContent: 'center',
paddingLeft: 40,
paddingRight: 40,
},
textInput: {
alignSelf: 'stretch',
paddingLeft: 16,
marginBottom: 20,
backgroundColor: '#fff',
},
btn: {
alignSelf: 'stretch',
padding: 20,
marginBottom: 20,
backgroundColor: '#01c853',
alignItems: 'center',
},
});