简体   繁体   English

AngularJS:返回承诺的响应-即上传图片然后提交表格

[英]AngularJS : Return response of a promise - ie image upload THEN submit a form

I'm trying to understand promises and $q in AngularJS. 我想了解AngularJS中的promise和$ q。 I'd like to chain 2 functions, and execute them in order. 我想链接2个函数,并按顺序执行它们。 The first is a file upload, and can take a while, so I need to wait for it to be executed before creating a DB record for it. 第一个是文件上传,可能需要一段时间,因此我需要等待其执行后才能为其创建数据库记录。 I was originally using angular 1.3, hence the .success and .error methods. 我最初使用的是angular 1.3,因此使用.success和.error方法。

Here's the code: 这是代码:

    //image upload function 

    $scope.imageUpload = function(){
        return $q(function (resolve, reject) {
              var fd = new FormData();
              fd.append("file", $scope.imageFile);
              var data = fd;
              var url = 'http://myApiUrl';
              var postObject = {
                  method: 'POST',
                  url: url,
                  data: data
              }
              $http(postObject)
                 .success(function(response) {
                     return response; 
              }).error(function(response) {
                     return response; 
              });
          })
      }

    // create a DB record 
    $scope.recordCreate = function(previousResponse){
          return $q(function (resolve, reject) {

               // does stuff with previousResponse, and creates a db record

            })
        };

    // do functions in order 

    $scope.imageUpload().then(
            $scope.recordCreate(response)
          );

Not only am I getting the error 我不仅收到错误

ReferenceError: response is not defined ReferenceError:未定义响应

(in relation to that last line, $scope.recordCreate(response)), but also the second function is executing BEFORE the first! (相对于最后一行$ scope.recordCreate(response)),第二个函数也在第一个函数之前执行!

I have tried to follow the docs here https://docs.angularjs.org/api/ng/service/ $q but in my example I can't get my first function to return a response to the 2nd. 我试图按照这里的文档https://docs.angularjs.org/api/ng/service/ $ q,但是在我的示例中,我无法使用第一个函数将响应返回到第二个。 Does anyone know why? 有人知道为什么吗?

You should use .then() instead of .success() to return your promise which you can then act upon and call the subsequent method. 您应该使用.success() .then()而不是.success()返回您的诺言,然后您可以对诺言进行操作并调用后续方法。

Also, I'm curious as to why you wouldn't just update the DB server-side on your file-upload call. 另外,我很好奇为什么您不只是在文件上传调用中更新数据库服务器端。

no need to use $q to execute http requests in order. 无需使用$q依次执行http请求。 use promise chains to call the request after another 使用诺言链来依次调用请求

$scope.imageUpload = function() {
    var fd = new FormData();
    fd.append("file", $scope.imageFile);
    var data = fd;
    var url = 'http://myApiUrl';
    var postObject = {
        method: 'POST',
        url: url,
        data: data
    }
    return $http(postObject)
}
$scope.recordCreate = function(previousResponse) {
    var url = 'db_url';
    var postObject = {
        method: 'POST',
        url: url,
        data: previousResponse
    }
    return $http(postObject)
};
$scope.imageUpload()
    .then(function(response) {
        return $scope.recordCreate(response.data)
    })
    .then(function(response) {
        console.log(response.data);
    })

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

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