首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PouchDB/Benchmark.js . to中,在推送到允诺数组之前修改对象的值

在PouchDB/Benchmark.js . to中,在推送到允诺数组之前修改对象的值
EN

Stack Overflow用户
提问于 2016-01-05 09:48:33
回答 1查看 144关注 0票数 0

我目前正在测试在PouchDB中插入多个不同的对象所需的时间,要么一个接一个地插入它们,要么执行批量插入。我的方法是检索一个大的生成的JSON项,并在循环中使用一个自定义函数更改_id,然后将执行插入的函数推送到承诺数组中,以最终调用数组上的Promise.all

测试本质上是异步的(需要延迟关键字)。

代码看起来像(coffeescript)

代码语言:javascript
复制
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之前,我还有另一个打印输出:

代码语言:javascript
复制
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次,但它似乎只使用最后一个值,即使它在推送函数之前打印了新的值。

EN

回答 1

Stack Overflow用户

发布于 2016-01-05 13:56:22

我只是解决了我自己的问题以防有人感兴趣。当为文档分配一个新的id时,它是对象引用被修改的对象,而不是新的对象。要解决这个问题:

代码语言:javascript
复制
docAux = Ember.copy doc
Object.assign docAux, { _id: @makeid() } # New id for each new item.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34608565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档