我使用科爾多瓦Facebook插件來獲取用戶名/電子郵件/等(工作正常),然後想要插入到我自己的數據庫。我已經通過創建一個函數來測試發佈到DB代碼,將它綁定到一個測試按鈕,並且工作正常。但是,當我將post-2-db代碼放在fb-login代碼中時,它會停止執行完全在我的http-post中。我做的console.log(步驟1),並看到很好,但我從來沒有看到的console.log(第二步) - 見下面我的代碼:Ionic2/Angular2不能調用函數內功能
// LOGIN TO FACEBOOK
doFbLogin(){
let permissions = new Array();
let nav = this.navCtrl;
//the permissions your facebook app needs from the user
permissions = ["public_profile"];
Facebook.login(permissions)
.then(function(response){
let userId = response.authResponse.userID;
let params = new Array();
//Getting name and gender properties
Facebook.api("/me?fields=name,gender,email", params)
.then(function(user) {
user.picture = "https://graph.facebook.com/" + userId + "/picture?type=large";
//now we have the users info, let's save it in the 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");
// IT WORKS 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
// I NEVER SEE THIS CONSOLE LOG BELOW
console.log("STEP 2");
// User Reg complete, push them to app
nav.push(TabsPage);
}, function (error) {
console.log(error);
})
})
}, function(error){
console.log(error);
});
}
}
如果我拿出的代碼塊,一切工作正常。我非常喜歡編碼,我會很感激任何透徹的外行人員的解釋和協助。謝謝。
如果您將您的問題看起來不像小說,更像是一段非常短的代碼,其中所有不相關的行被刪除,您將得到更好的答案。 – Ari