2015-08-21 17 views
0

我正在使用後臺作業來查詢具有數千個對象的json以最初填充我的數據庫。我也實現了beforesave函數來防止任何重複的條目。但是,一旦我實現了這個,看起來我的後臺作業叫做response.error,並不保存所有的對象。看起來我可能超過請求/秒?如果有人可以看看我的代碼並告訴我爲什麼它不能成功保存所有條目,我將不勝感激。Failing在使用數千個對象之前使用beforesave時解析後臺作業

這裏是我的後臺作業:

Parse.Cloud.job("testing", function(request, response) { 

    var json; 

    Parse.Cloud.httpRequest({ 
     url: stringURL + pageNumber.toString(), 
     success: function(httpResponse) { 

     json = httpResponse.data; 
     console.log("total is: " + json["meta"].total); 
     console.log("object 1 is: " + json["events"][1].title); 
     return json; 
     } 
    //after getting the json, save all 1000 
    }).then(function() { 
//helper function called 
     saveObjects(json).then(function() { 
      response.success("success"); 
     }, 
     function(error) { 
      response.error("nooooo"); 
     }); 
    }); 
}); 

function saveObjects(json) { 
    var promises = []; 
    for(var i = 0; i < 1000; i++) { 
     var newEvent = new Event(); 
     promises.push(newEvent.save(new Event(json["events"][i]))); 
    } 
    return Parse.Promise.when(promises); 
} 

這裏是我的beforesave代碼:

Parse.Cloud.beforeSave("Event", function(request, response) { 
    var newEvent = request.object; 
    var Event = Parse.Object.extend("Event"); 
    var query = new Parse.Query("Event"); 

    query.equalTo("title", newEvent.get("title")); 
    query.equalTo("datetime_utc", newEvent.get("datetime_utc")); 
    query.equalTo("url", newEvent.get("url")); 
    query.first({ 

    success: function(temp) { 
     response.error({errorCode:123,errorMsg:"Event already exist!"});   
    }, 
    error: function(error) { 
     response.success(); 
    } 
    }); 
}); 

感謝我真的很感激任何幫助......我一直堅持了一段時間所。

回答

0

如果是請求率問題,那麼您可能可以使用類似node-function-rate-limit的東西,但編寫自己的速率限制批處理器相當簡單。請參閱下面的doInBatches()

此外,當使用還提供「成功:...」回調的承諾返回方法時,最好不要混合這兩種樣式。它可能會像預期的那樣行事,但你被剝奪了將「成功:......」回調的結果傳遞給承諾鏈其餘部分的機會。正如你可以在下面看到的,「成功:...」代碼簡單地被拖入.then()回調。

Parse.Cloud.job("testing", function(request, response) { 
    Parse.Cloud.httpRequest({ 
     url: stringURL + pageNumber.toString() 
    }).then(function(httpResponse) { 
     var json = httpResponse.data; 
     // console.log("total is: " + json.meta.total); 
     // console.log("object 1 is: " + json.events[1].title); 
     /* helper function called */ 
     doInBatches(json.events, 30, 1000, function(evt, i) { 
       var newEvent = new Event(); 
      return newEvent.save(new Event(evt)); 
     }).then(function() { 
      response.success('success'); 
     }, function(error) { 
      response.error('nooooo'); 
     }); 
    }); 
}); 

// Async batcher. 
function doInBatches(arr, batchSize, delay, fn) { 
    function delayAsync() { 
     var p = new Parse.Promise(); 
     setTimeout(p.resolve, delay); 
     return p; 
    } 
    function saveBatch(start) { 
     if(start < arr.length) { 
      return Parse.Promise.when(arr.slice(start, start+batchSize).map(fn)) 
      .then(delayAsync) // delay between batches 
      .then(function() { 
       return saveBatch(start + batchSize); 
      }); 
     } else { 
      return Parse.Promise.as(); 
     } 
    } 
    return saveBatch(0); 
} 

我看不到如何或爲什麼beforesave代碼可能會影響的東西。

相關問題