通过promises上传图片,bluebird pass params

时间:2015-06-09 07:12:48

标签: javascript return promise bluebird

我需要在Amazon S3上传图像,并将作为响应收到的图像链接存储到数据库中。 我正在使用Bluebird.js。请告诉我如何将从一个Promise记录的值传递给另一个Promise。

Promise.resolve()
.then(function() {
    var promImageLoc = [];
    var upload = $scope.uploaderPromo.queue;
    $scope.uploaderPromo.uploadAll();
    $scope.uploaderPromo.onSuccessItem = function(fileItem, response, status, headers) {
        console.log('success response', response);
        var promImage = promImageLoc.push(response.location);
        return promImage;
    }
    console.log(promImage);
})
.then(function (promImageLoc) {
    console.log(promImageLoc);
})

更新:我在MEAN堆栈开发人员处理,并使用angular-file-uploadmulter将文件从客户端传递到服务器。现在我只需要(是爱) - 使用以下链接在mongo DB中输入。链接一个接一个地回调,因为文件是逐个发送的(POST),我终于进入但是有空数组。我知道原因,但我不知道如何解决它。以下是从客户端发送数据的完整功能代码。

$scope.createPromo = function () {
            Promise.resolve()
            .then(function() {
                var promImageLoc = [];
                var upload = $scope.uploaderPromo.queue;
                $scope.uploaderPromo.uploadAll();
                async.eachSeries(upload, function(response) {
                    $scope.uploaderPromo.onSuccessItem = function(fileItem, response, status, headers) {
                        console.log('success response', response);
                        return promImageLoc.push(response.location);
                    }
                })
            })
            .then(function() {
                var promLogoLoc = [];
                $scope.uploaderLogo.uploadAll();
                $scope.uploaderLogo.onSuccessItem = function(fileItem, response, status, headers) {
                    console.log('success response', response);
                    var location = response.location;
                    return location;
                }
                promLogoLoc.push(location);
                return promLogoLoc;
            })
            .then(function (promImageLoc, promLogoLoc) {
                if ($scope.promZipCode == undefined) {
                    $scope.promZipCode = '07946';
                }
                $http.post("/promo/create", {
                    'promName':     $cookieStore.get('User').user.username + '.' + $scope.promName, //username.promotion name
                    'promDesc':     $scope.promDesc,
                    'promImage':    promImageLoc,
                    'promLock':     $scope.promLock,
                    'promLogo':     promLogoLoc,
                    'promLogoPos':  $scope.selectPos,
                    'promNotice':   $scope.promNotice,
                    'promZipCode':  $scope.promZipCode, //{obj}
                    'promRadius':   $scope.promRadius
                })
                .success(function (data, status) {
                    console.log(status);
                    $state.go('home.promotion.promotions');
                }).error(function (err) {
                    $scope.isUniqError = true;
                    console.log('CREATE ERR', err);
                });
            })
            .catch(function (error) {
                console.log('ERROR PROMISE:', error);
            })

0 个答案:

没有答案