简体   繁体   English

$ http.get在执行$ http.post之后返​​回旧查询,我在做什么错?

[英]$http.get returning an old query after doing $http.post, what am i doing wrong?

I'm a newbee in angular, currenty I'm working in a website that saves the score of players in a postgres database, before inserting in the table "Puntaje" I have to save the id of the game and the id of the player into another table, my code actually does that, but when I try to retrieve the last inserted ID in such table, it returns a query as if it didn't insert the key, although in the database it shows that it was inserted, here is the code: 我是新手,目前正在网站上,将球员的得分保存在postgres数据库中,然后在表格“ 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);
    }))
}))}

Why does it happen? 为什么会发生? How can I fix it? 我该如何解决? Thanks in advance. 提前致谢。

$http.post and $http.get are asynchronous. $http.post$http.get是异步的。 You need to call get after post is finished. 发布结束后,您需要致电get。 Use then after post and inside callback call get. 在post和内部回调调用get之后使用then

As you are making multiple asynchronous requests which are depending on the result of each other you need to make sure that each and every request is completed before moving on to the next one. 当您发出多个异步请求时,这些请求取决于彼此的结果,因此您需要确保每个请求都已完成,然后再继续下一个请求。

To do this you can use the promise that $http returns and add the depending calls in the then callback function as this 为此,您可以使用$ http返回的承诺,并在then回调函数中添加依赖调用,如下所示

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);
                    });
                });
        });
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM