2016-01-05 14 views
0

我目前正在測試在PouchDB中插入多個不同對象所需的時間,這些時間既可以逐個插入也可以執行批量插入。 我的方法是檢索生成的JSON項目並使用循環內的自定義函數更改該項目,然後將執行插入操作的函數推送到promises數組中,最後在整個數組上調用Promise.all在PouchDB/Benchmark.js中推送承諾數組之前修改對象的值

測試本質上是異步的(推遲關鍵字需要)。

的代碼看起來像(CoffeeScript的)

benchmarkCreate:() -> 
    result = {} 
    Ember.$.getJSON('bigitem.json').then((doc) => 
    @createUpdateTest 10, result, doc[0] # Takes the JSON only 
) 

createUpdateTest: (many, res, doc) -> 
(@get 'benchSuite').add(new Benchmark("Create #{many} Items Bench", 
    'defer': true 

    fn: (deferred) => 
    iterations = [] 
    i = 0 
    while i < many 
     doc._id = @makeid() 
     console.log 'Out of the Promise array: ' + doc._id 
     iterations.push ((@get 'pouchService').createUpdateDoc doc) 
     i++ 
    Promise.all(iterations).then((response) -> 
     deferred.resolve() 
    ) 
)) 

pouchService被認爲成功與插入不同的ID的新項目(管理不需要修訂)。我還有一個打印只是把文件中PouchDB之前和輸出我擁有的是:

Out of the Promise array: z2sF0 
Out of the Promise array: v2J3F 
Out of the Promise array: MY2qX  
Out of the Promise array: BkKiv 
Out of the Promise array: DjcUK 
Out of the Promise array: TIL6e 
Out of the Promise array: xjz20 
Out of the Promise array: oHAFf 
Out of the Promise array: dWK6U 
Out of the Promise array: 9KKRi 

Inside the Promise Array: 9KKRi 
Inside the Promise Array: 9KKRi 
Inside the Promise Array: 9KKRi 
... X 10 

我需要推入承諾陣列比之前的ID修改10次,但它似乎只需要最後一個值,即使它在推送功能之前打印新的一個。

回答

0

我剛解決了我自己的問題,以防有人可能感興趣。當爲文檔分配一個新的ID時,它是對象引用被修改的對象,而不是新對象。解決它:

docAux = Ember.copy doc 
Object.assign docAux, { _id: @makeid() } # New id for each new item.