Parse.com Cloud Code - saveAll请求超时

时间:2012-12-26 14:28:41

标签: javascript parse-platform

我正在尝试从Cloud Code功能中保存一组对象。

在30-40个对象之后,我得到一个超时错误。我的代码如下所示:

Parse.Cloud.define("saveInBackground", function (request, response) {
    console.log("saveInBackground begin");

    var objectsToSave = [];

    for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) {
        objectsToSave.push(new LikedObject(request.params.collectionToSave[i])); 
    };

    Parse.Object.saveAll(objectsToSave, {
        success: function(list) {
            // All the objects were saved.
            if (response) {
                response.success(list);
            };

            console.log("saveInBackground success");
        },
        error: function(model, error) {
            // An error occurred while saving one of the objects.
            if (response) {
                response.error(error);
            };

            console.log("saveInBackground error: " + error.message);
        }
    });

    console.log("saveInBackground end");
});

我可以为保存一堆物品而做其他事吗?

1 个答案:

答案 0 :(得分:4)

根据对象的大小,您可以尝试以20-30的批量保存它们。这是必需的,因为saveAll()正在尝试将您提供给它的所有对象上传到服务器。以下是如何执行此操作的示例代码:

var result = true;
for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) {
    objectsToSave.push(new LikedObject(request.params.collectionToSave[i])); 
    if (i % 10 == 0) {
        result = saveObjects(objectsToSave);
        objectsToSave.length = 0;
    }
};
if (result == true) {
    console.log("saveInBackground success");
}


function saveObjects(objects) {

    Parse.Object.saveAll(...);
    ...
}