AngularJS变量从第一个响应未传递给第二个请求?

时间:2016-11-18 08:29:29

标签: angularjs parse-platform

我使用Parse.com作为后端,我需要发送两次帖子,首先是_User类和customer类。 Customer类有user_id字段,指向_User.objectId

该方案是我发送帖子到_User获取objectId,并将帖子发送到包含customer objectId的_User

这是我的代码

    $http.post('http://128.199.249.xxx:1337/parse/users', data, configRegister).then(function (response) {
    console.log(response);  
    $scope.userObjectId = response.data.objectId;
    console.log($scope.userObjectId); //succes print objectId
}, function (error) {
    alert(error.data.error);
});


var dataProfileCustomer = {
    user_id : {
        __type: "Pointer",
        className: "_User",
        objectId: $scope.userObjectId //this part is not exist when I check in post request
    },
    family_name: $scope.customer.family_name,
    family_phone: $scope.customer.family_phone,
    family_address: $scope.customer.family_address
};

$http.post('http://128.199.249.xxx:1337/parse/classes/customer', dataProfileCustomer, configRegister).then(function (response) {
    console.log(response.data); 
}, function (error) {
    alert(error.data.error);
});

上面的代码结果成功插入了2个类,customer.user_id未定义。

我的代码遗漏或错误了吗?

1 个答案:

答案 0 :(得分:0)

您必须在第一个http.post请求的成功响应中写入第二个http.post请求代码。

$http.post('http://128.199.249.xxx:1337/parse/users', data, configRegister).then(
function (response) {
    console.log(response);  
    $scope.userObjectId = response.data.objectId;
    console.log($scope.userObjectId); //succes print objectId

    var dataProfileCustomer = {
        user_id : {
            __type: "Pointer",
            className: "_User",
            objectId: $scope.userObjectId //this part is not exist when I check in post request
        },
        family_name: $scope.customer.family_name,
        family_phone: $scope.customer.family_phone,
        family_address: $scope.customer.family_address
    };
    $http.post('http://128.199.249.xxx:1337/parse/classes/customer', dataProfileCustomer, configRegister).then(
    function (response) {
        console.log(response.data); 
    }, function (error) {
        alert(error.data.error);
    });


}, function (error) {
    alert(error.data.error);
});