云功能不断循环,直到I / O故障

时间:2016-08-23 14:30:19

标签: javascript parse-platform dropbox-api

我尝试制作云计算功能,将图片上传到Dropbox,然后创建并上传摘要文本文件:

Parse.Cloud.define("transferToStore",function(request, response) {
Parse.Cloud.useMasterKey();

var token;
var tokenQuery = new Parse.Query("StoreTokens");
tokenQuery.equalTo("store", request.params.store);
tokenQuery.find().then(function(results) {
    token = results[0].get("token");
}).then(function() {
    var photoQuery = new Parse.Query("Uploads");
    photoQuery.equalTo("orderNumber", request.params.orderNumber);
    photoQuery.equalTo("store", request.params.store);
    photoQuery.find({
        success: function(results) {
            for (var i = 0; i < results.length; ++i) {
                var photo = results[i].get("photo");
                var photoBuffer;
                Parse.Cloud.httpRequest({
                    url: photo.url()
                }).then(function(bufferResponse) {
                    photoBuffer = bufferResponse.buffer;
                    console.log('buffer has been generated: ');
                    var dropboxParams = new Object();
                    dropboxParams.path = '/' + request.params.orderNumber + '/' + results[i].get("size") + '/' + results[i].get("fileName");
                    dropboxParams.mode = 'add';
                    dropboxParams.autorename = true;
                    dropboxParams.mute = false;

                    console.log('accessing buffer: ');

                     // This part stopped working
                    Parse.Cloud.httpRequest({
                        method: 'POST',
                        url: 'https://content.dropboxapi.com/2/files/upload',
                        headers: {
                            'Authorization': 'Bearer ' + token,
                            'Dropbox-API-Arg': JSON.stringify(dropboxParams),
                            'Content-Type': 'application/octet-stream'
                        },
                        body: photoBuffer
                    }).then(function(httpResponse) {
                        console.log('upload - response:  status: ' + httpResponse.status + 'text: ' + httpResponse.text);
                    }, function(httpResponse) {
                        console.log('upload failed: status: ' + httpResponse.status + 'error: ' + httpResponse.text);
                        response.error('status: ' + httpResponse.status + 'error: ' + httpResponse.text + ' ');
                    });
                }, function(bufferResponse) {
                    console.log('buffer response failed');
                });
            }
        },
        error: function() {
            response.error('uploads query failed');
        }
    });

    //this part works and is within a loop until function returns i/o failure
    var Order = Parse.Object.extend("Orders");
    var order = new Order();
    order.set("store", request.params.store);
    order.set("orderNumber", request.params.orderNumber);
    order.set("name", request.params.name);
    order.set("phone", request.params.phone);
    order.set("email", request.params.email);
    order.set("type", request.params.type);
    order.set("payment", request.params.payment);
    var str = "number: " + request.params.orderNumber + "\r\n"
                + "ordered by: " + request.params.name + "\r\n"
                + "phone: " + request.params.phone + "\r\n"
                + "email: " + request.params.email + "\r\n";
    var buff = new Buffer(str, 'utf8').toString("base64");
    var file = new Parse.File("order.txt", { base64: buff }, 'text/plain');
    order.set("summary", file);
    order.save().then(function() {
        Parse.Cloud.httpRequest({
            method: 'POST',
            url: 'https://api.dropboxapi.com/2/files/save_url',
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Type': 'application/json; charset=utf-8'
            },
            body: {
                'path': '/' + request.params.orderNumber + '/' + 'order.txt',
                'url': file.url()
            }
        });
        respone.success('ok');
    }, function() {
        console.log('summary save failed');
        response.error('summary save failed');
    });
});

}); 我在代码中添加了两条注释来尝试解释问题 在开始时摘要文件工作正常,只有一次和Dropbox上传工作,但文件是空的,因为代码的一部分是在文件缓冲区后,但由于文件缓冲区没有生成它上传空文件。 所以我将上传的httprequest移到了文件缓冲区httprequest中,然后问题就开始了。 我也尝试过承诺并遇到同样的问题。

我想这个问题是关于缺乏响应的问题。但是无法理解在哪里。

0 个答案:

没有答案