Currently, I am using the Cordova Facebook Plugin to retrieve user information such as name and email, which is functioning correctly. My next step is to insert this data into my own database. Testing the code for posting to the database by creating a function that is linked to a test button has been successful. However, when I try to include the post-2-db code within the fb-login code, it stops executing right at the http-post. I have inserted console.log(step1) to check progress, but I never see console.log(step2). Below is the snippet of my code:
// LOGIN TO FACEBOOK
doFbLogin(){
let permissions = new Array();
let nav = this.navCtrl;
// Permissions required by the Facebook app
permissions = ["public_profile"];
Facebook.login(permissions)
.then(function(response){
let userId = response.authResponse.userID;
let params = new Array();
// Retrieving user's name, gender, and email properties
Facebook.api("/me?fields=name,gender,email", params)
.then(function(user) {
user.picture = "https://graph.facebook.com/" + userId + "/picture?type=large";
// Saving user info in NativeStorage
NativeStorage.setItem('user',
{
name: user.name,
gender: user.gender,
picture: user.picture,
email: user.email,
})
.then(function(){
// BEGIN INSERT USER TO DB
// postRequest() {
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json' );
let options = new RequestOptions({ headers: headers });
let postParams = {
userName: user.name,
userEmail: user.email,
userImage: user.picture,
}
console.log("STEP 1");
// CODE EXECUTES UP TO THIS POINT
this.http.post("http://example.com/post.php", postParams, options)
.subscribe(data => {
console.log(data['_body']);
}, error => {
console.log(error);// Error getting the data
});
//}
// END DB INSERT CODE
// CONSOLE LOG BELOW IS NOT DISPLAYED
console.log("STEP 2");
// User registration complete, direct to app
nav.push(TabsPage);
}, function (error) {
console.log(error);
})
})
}, function(error){
console.log(error);
});
}
}
If I remove that section of code, everything runs smoothly. As a programming novice, any simple explanation and assistance would be greatly appreciated. Thank you.