2016-12-04 2 views
1

私は今、角度の新人です私はテーブル "Puntaje"に挿入する前に、ポストグレスデータベースにプレーヤーのスコアを保存するウェブサイトで働いています私はゲームのIDとIDを保存する必要があります私のコードは実際にそれを行いますが、そのようなテーブルに最後に挿入されたIDを取得しようとすると、キーが挿入されていないかのようにクエリが返されますが、ここにコードがあります:

angular.module('clienteJuegoApp').controller('Juego1Ctrl', function ($scope, $http){ 
$scope.juego = "Survive"; 
$scope.add = function(){ 

    //First part, saves the gameID and the Player ID into the 1st table 
    console.log($http.get('http://127.0.0.1:8080/v1/jugador').then(function(response) { 
    console.log($scope.d = response.data); 
    console.log("long"+$scope.d.length); 
    console.log("idjugaor"+$scope.d[$scope.d.length-1].Id); 
    var datosJuegoJugador={ 
     idjuego: {Id:1}, 
     idjugaor:{Id:$scope.d[$scope.d.length-1].Id} 
    }; 
    $http.post("http://127.0.0.1:8080/v1/juegojugador", datosJuegoJugador); 
    console.log("se inserto"); 


    //Second part, retrieve the last inserted Id of the previous table 

    console.log($http.get('http://127.0.0.1:8080/v1/juegojugador').then(function(response2) { 
    console.log($scope.dj = response2.data) 
    console.log("idjuegojugador:"+$scope.dj[$scope.dj.length-1].Id) 

    var data = { 
     Idjuegojugador: {Id: $scope.dj[$scope.dj.length-1].Id}, 
     Puntaje: parseInt($("input:text[name=puntaje]").val()) 
    }; 

    console.log(data) 
    $http.post("http://127.0.0.1:8080/v1/puntaje", data); 
    })) 
}))} 

どうしてですか?どうすれば修正できますか? ありがとうございます。

答えて

1

非同期の要求に応じて、それぞれの要求が完了してから次の要求に進む前に完了する必要があります。あなたが約束$ HTTPリターンを使用し、それは動作しませんでした。この

angular.module('clienteJuegoApp').controller('Juego1Ctrl', function ($scope, $http){ 
    $scope.juego = "Survive"; 
    $scope.add = function(){ 

     $http.get('http://127.0.0.1:8080/v1/jugador') 
      .then(function(response) { 
       $scope.d = response.data); 
       var datosJuegoJugador={ 
        idjuego: {Id:1}, 
        idjugaor:{Id:$scope.d[$scope.d.length-1].Id} 
       }; 

       $http.post("http://127.0.0.1:8080/v1/juegojugador", datosJuegoJugador).then(function(response2) { 

        $http.get('http://127.0.0.1:8080/v1/juegojugador').then(function(response3) { 
         $scope.dj = response3.data 
         var data = { 
          Idjuegojugador: {Id: $scope.dj[$scope.dj.length-1].Id}, 
          Puntaje: parseInt($("input:text[name=puntaje]").val()) 
         }; 
         $http.post("http://127.0.0.1:8080/v1/puntaje", data); 
        }); 
       }); 
     }); 
    }); 

}); 
+0

ありがとう、それは魅力のように動作します! –

+0

素晴らしい:-)良い一日を –

1

$http.postおよび$http.getは非同期です。投稿が終わった後にgetを呼び出す必要があります。ポストコールバックコールとコールバックコールコール取得後にthenを使用します。

+0

として、コールバック関数内応じて呼び出しを追加できます。これを行うには

...あなたはそれについての詳細を説明することができ、してください? –

関連する問題