How To Insert to Server After Doing Something
What I am trying to do is when the page is loaded up, it will call the this.getKey()
method and after it is called, it will call again the this.InsertDataToServer
to populate the text fields. However, currently that is not working and all the text fields are blank since this.InsertDataToServer
is never called. What is my problem in my code?
import React, { Component } from 'react';
import {
Text,
View,
TextInput,
StatusBar,
Alert
} from 'react-native';
import { Card, Button, Header, Left, Body, Right, Title, Form, Label, Item, Input} from 'native-base';
import {AsyncStorage} from 'react-native';
export default class Profile extends Component {
componentWillMount(){
this.getKey().then(()=>{
this.InsertDataToServer
});
}
constructor(props) {
super(props)
this.state = {
mysessionUsername:null,
username: '',
firstName: '',
lastName: '',
emailAddress: '',
phoneNumber: '',
Address: '',
parseMe: null
}
}
async getKey() {
try {
AsyncStorage.getItem('sessionUsername').then((value) => {
this.setState({"mysessionUsername": value});
})
.then(res => {
// Alert.alert(this.state.mysessionUsername);
});
//await this.setState({ lchMoteurRch: AsyncVal });
} catch (error) {
console.log("Error retrieving data" + error);
}
}
async saveKey(value) {
try {
await AsyncStorage.setItem('sessionUsername', value);
} catch (error) {
console.log("Error saving data" + error);
}
}
InsertDataToServer = () =>{
//const { TextUserName } = this.state ;
fetch('someLink.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.mysessionUsername,
})
})
.then((response) => response.json())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
this.state.parseMe = responseJson;
this.state.parseMe = this.state.parseMe.split(" ");
this.setState({
"username": this.state.parseMe[0],
"firstName": this.state.parseMe[1],
"lastName": this.state.parseMe[2],
"emailAddress": this.state.parseMe[3],
"phoneNumber": this.state.parseMe[4],
"Address": this.state.parseMe[5]
});
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View>
<Header style={styles.header}>
<Left>
<Title>Account</Title>
</Left>
<Right>
</Right>
</Header>
<StatusBar
backgroundColor='#000000'
/>
<Card>
<Text style={styles.textStyle}>Username: {this.state.username}</Text>
<Text style={styles.textStyle}>First Name: {this.state.firstName}</Text>
<Text style={styles.textStyle}>Last Name: {this.state.lastName}</Text>
<Text style={styles.textStyle}>Email Address: {this.state.emailAddress}</Text>
<Text style={styles.textStyle}>Phone Number: {this.state.phoneNumber}</Text>
<Text style={styles.textStyle}>Address: {this.state.Address}</Text>
</Card>
<Button
full
rounded
success
style={styles.button}
onPress={() => this.props.navigation.navigate('LoginForm')}
>
<Text style={{ color: 'white' }}>LOG OUT</Text>
</Button>
</View>
);
}
}
const styles = {
textStyle: {
fontSize: 25,
textAlign: 'center',
marginTop: 10,
marginBottom: 10,
fontWeight: 'bold'
},
button: {
marginTop: 20,
marginLeft: 40,
marginRight: 40
},
header: {
backgroundColor: '#00b3b3',
color: 'white'
}
};
react-native
add a comment |
What I am trying to do is when the page is loaded up, it will call the this.getKey()
method and after it is called, it will call again the this.InsertDataToServer
to populate the text fields. However, currently that is not working and all the text fields are blank since this.InsertDataToServer
is never called. What is my problem in my code?
import React, { Component } from 'react';
import {
Text,
View,
TextInput,
StatusBar,
Alert
} from 'react-native';
import { Card, Button, Header, Left, Body, Right, Title, Form, Label, Item, Input} from 'native-base';
import {AsyncStorage} from 'react-native';
export default class Profile extends Component {
componentWillMount(){
this.getKey().then(()=>{
this.InsertDataToServer
});
}
constructor(props) {
super(props)
this.state = {
mysessionUsername:null,
username: '',
firstName: '',
lastName: '',
emailAddress: '',
phoneNumber: '',
Address: '',
parseMe: null
}
}
async getKey() {
try {
AsyncStorage.getItem('sessionUsername').then((value) => {
this.setState({"mysessionUsername": value});
})
.then(res => {
// Alert.alert(this.state.mysessionUsername);
});
//await this.setState({ lchMoteurRch: AsyncVal });
} catch (error) {
console.log("Error retrieving data" + error);
}
}
async saveKey(value) {
try {
await AsyncStorage.setItem('sessionUsername', value);
} catch (error) {
console.log("Error saving data" + error);
}
}
InsertDataToServer = () =>{
//const { TextUserName } = this.state ;
fetch('someLink.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.mysessionUsername,
})
})
.then((response) => response.json())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
this.state.parseMe = responseJson;
this.state.parseMe = this.state.parseMe.split(" ");
this.setState({
"username": this.state.parseMe[0],
"firstName": this.state.parseMe[1],
"lastName": this.state.parseMe[2],
"emailAddress": this.state.parseMe[3],
"phoneNumber": this.state.parseMe[4],
"Address": this.state.parseMe[5]
});
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View>
<Header style={styles.header}>
<Left>
<Title>Account</Title>
</Left>
<Right>
</Right>
</Header>
<StatusBar
backgroundColor='#000000'
/>
<Card>
<Text style={styles.textStyle}>Username: {this.state.username}</Text>
<Text style={styles.textStyle}>First Name: {this.state.firstName}</Text>
<Text style={styles.textStyle}>Last Name: {this.state.lastName}</Text>
<Text style={styles.textStyle}>Email Address: {this.state.emailAddress}</Text>
<Text style={styles.textStyle}>Phone Number: {this.state.phoneNumber}</Text>
<Text style={styles.textStyle}>Address: {this.state.Address}</Text>
</Card>
<Button
full
rounded
success
style={styles.button}
onPress={() => this.props.navigation.navigate('LoginForm')}
>
<Text style={{ color: 'white' }}>LOG OUT</Text>
</Button>
</View>
);
}
}
const styles = {
textStyle: {
fontSize: 25,
textAlign: 'center',
marginTop: 10,
marginBottom: 10,
fontWeight: 'bold'
},
button: {
marginTop: 20,
marginLeft: 40,
marginRight: 40
},
header: {
backgroundColor: '#00b3b3',
color: 'white'
}
};
react-native
can you try to add brackets to function call? fromthis.InsertDataToServer
to thisthis.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
That did not work
– johnbumble
Nov 25 '18 at 20:27
add a comment |
What I am trying to do is when the page is loaded up, it will call the this.getKey()
method and after it is called, it will call again the this.InsertDataToServer
to populate the text fields. However, currently that is not working and all the text fields are blank since this.InsertDataToServer
is never called. What is my problem in my code?
import React, { Component } from 'react';
import {
Text,
View,
TextInput,
StatusBar,
Alert
} from 'react-native';
import { Card, Button, Header, Left, Body, Right, Title, Form, Label, Item, Input} from 'native-base';
import {AsyncStorage} from 'react-native';
export default class Profile extends Component {
componentWillMount(){
this.getKey().then(()=>{
this.InsertDataToServer
});
}
constructor(props) {
super(props)
this.state = {
mysessionUsername:null,
username: '',
firstName: '',
lastName: '',
emailAddress: '',
phoneNumber: '',
Address: '',
parseMe: null
}
}
async getKey() {
try {
AsyncStorage.getItem('sessionUsername').then((value) => {
this.setState({"mysessionUsername": value});
})
.then(res => {
// Alert.alert(this.state.mysessionUsername);
});
//await this.setState({ lchMoteurRch: AsyncVal });
} catch (error) {
console.log("Error retrieving data" + error);
}
}
async saveKey(value) {
try {
await AsyncStorage.setItem('sessionUsername', value);
} catch (error) {
console.log("Error saving data" + error);
}
}
InsertDataToServer = () =>{
//const { TextUserName } = this.state ;
fetch('someLink.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.mysessionUsername,
})
})
.then((response) => response.json())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
this.state.parseMe = responseJson;
this.state.parseMe = this.state.parseMe.split(" ");
this.setState({
"username": this.state.parseMe[0],
"firstName": this.state.parseMe[1],
"lastName": this.state.parseMe[2],
"emailAddress": this.state.parseMe[3],
"phoneNumber": this.state.parseMe[4],
"Address": this.state.parseMe[5]
});
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View>
<Header style={styles.header}>
<Left>
<Title>Account</Title>
</Left>
<Right>
</Right>
</Header>
<StatusBar
backgroundColor='#000000'
/>
<Card>
<Text style={styles.textStyle}>Username: {this.state.username}</Text>
<Text style={styles.textStyle}>First Name: {this.state.firstName}</Text>
<Text style={styles.textStyle}>Last Name: {this.state.lastName}</Text>
<Text style={styles.textStyle}>Email Address: {this.state.emailAddress}</Text>
<Text style={styles.textStyle}>Phone Number: {this.state.phoneNumber}</Text>
<Text style={styles.textStyle}>Address: {this.state.Address}</Text>
</Card>
<Button
full
rounded
success
style={styles.button}
onPress={() => this.props.navigation.navigate('LoginForm')}
>
<Text style={{ color: 'white' }}>LOG OUT</Text>
</Button>
</View>
);
}
}
const styles = {
textStyle: {
fontSize: 25,
textAlign: 'center',
marginTop: 10,
marginBottom: 10,
fontWeight: 'bold'
},
button: {
marginTop: 20,
marginLeft: 40,
marginRight: 40
},
header: {
backgroundColor: '#00b3b3',
color: 'white'
}
};
react-native
What I am trying to do is when the page is loaded up, it will call the this.getKey()
method and after it is called, it will call again the this.InsertDataToServer
to populate the text fields. However, currently that is not working and all the text fields are blank since this.InsertDataToServer
is never called. What is my problem in my code?
import React, { Component } from 'react';
import {
Text,
View,
TextInput,
StatusBar,
Alert
} from 'react-native';
import { Card, Button, Header, Left, Body, Right, Title, Form, Label, Item, Input} from 'native-base';
import {AsyncStorage} from 'react-native';
export default class Profile extends Component {
componentWillMount(){
this.getKey().then(()=>{
this.InsertDataToServer
});
}
constructor(props) {
super(props)
this.state = {
mysessionUsername:null,
username: '',
firstName: '',
lastName: '',
emailAddress: '',
phoneNumber: '',
Address: '',
parseMe: null
}
}
async getKey() {
try {
AsyncStorage.getItem('sessionUsername').then((value) => {
this.setState({"mysessionUsername": value});
})
.then(res => {
// Alert.alert(this.state.mysessionUsername);
});
//await this.setState({ lchMoteurRch: AsyncVal });
} catch (error) {
console.log("Error retrieving data" + error);
}
}
async saveKey(value) {
try {
await AsyncStorage.setItem('sessionUsername', value);
} catch (error) {
console.log("Error saving data" + error);
}
}
InsertDataToServer = () =>{
//const { TextUserName } = this.state ;
fetch('someLink.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.mysessionUsername,
})
})
.then((response) => response.json())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
this.state.parseMe = responseJson;
this.state.parseMe = this.state.parseMe.split(" ");
this.setState({
"username": this.state.parseMe[0],
"firstName": this.state.parseMe[1],
"lastName": this.state.parseMe[2],
"emailAddress": this.state.parseMe[3],
"phoneNumber": this.state.parseMe[4],
"Address": this.state.parseMe[5]
});
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View>
<Header style={styles.header}>
<Left>
<Title>Account</Title>
</Left>
<Right>
</Right>
</Header>
<StatusBar
backgroundColor='#000000'
/>
<Card>
<Text style={styles.textStyle}>Username: {this.state.username}</Text>
<Text style={styles.textStyle}>First Name: {this.state.firstName}</Text>
<Text style={styles.textStyle}>Last Name: {this.state.lastName}</Text>
<Text style={styles.textStyle}>Email Address: {this.state.emailAddress}</Text>
<Text style={styles.textStyle}>Phone Number: {this.state.phoneNumber}</Text>
<Text style={styles.textStyle}>Address: {this.state.Address}</Text>
</Card>
<Button
full
rounded
success
style={styles.button}
onPress={() => this.props.navigation.navigate('LoginForm')}
>
<Text style={{ color: 'white' }}>LOG OUT</Text>
</Button>
</View>
);
}
}
const styles = {
textStyle: {
fontSize: 25,
textAlign: 'center',
marginTop: 10,
marginBottom: 10,
fontWeight: 'bold'
},
button: {
marginTop: 20,
marginLeft: 40,
marginRight: 40
},
header: {
backgroundColor: '#00b3b3',
color: 'white'
}
};
react-native
react-native
edited Nov 25 '18 at 10:04
kit
1,1063816
1,1063816
asked Nov 25 '18 at 4:16
johnbumblejohnbumble
1151310
1151310
can you try to add brackets to function call? fromthis.InsertDataToServer
to thisthis.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
That did not work
– johnbumble
Nov 25 '18 at 20:27
add a comment |
can you try to add brackets to function call? fromthis.InsertDataToServer
to thisthis.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
That did not work
– johnbumble
Nov 25 '18 at 20:27
can you try to add brackets to function call? from
this.InsertDataToServer
to this this.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
can you try to add brackets to function call? from
this.InsertDataToServer
to this this.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
That did not work
– johnbumble
Nov 25 '18 at 20:27
That did not work
– johnbumble
Nov 25 '18 at 20:27
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464614%2fhow-to-insert-to-server-after-doing-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464614%2fhow-to-insert-to-server-after-doing-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you try to add brackets to function call? from
this.InsertDataToServer
to thisthis.InsertDataToServer()
– akcoban
Nov 25 '18 at 10:10
That did not work
– johnbumble
Nov 25 '18 at 20:27