通过延迟补偿的流星更新回调快捷方式



我的"事务"模型中有这些方法。它们同时出现在客户端和服务器上:

Meteor.methods
  addMatching: (invoice, transaction) ->
    amount_open = if transaction.amount_open then transaction.amount_open else transaction.amount
    amount = Math.min(invoice.amount_open,amount_open)
    invoice_nbr = if invoice.invoice_nbr then invoice.invoice_nbr else "999999"
    Transactions.update(transaction._id, {$push:{matchings:{invoice_id: invoice._id, invoice_nbr: invoice_nbr, amount: amount }}}, (e, r) ->
      if e
        console.log e
      else
        Invoices.update(invoice._id, {$inc: {amount_open: - amount}})
        Meteor.call "updateAmountOpen", transaction
    )

  updateAmountOpen: (transaction) ->
    amount_matched = 0
    transaction.matchings.map (matching) ->
      amount_matched = amount_matched + matching.amount
    total = transaction.amount - amount_matched
    Transactions.update(transaction._id, {$set: {amount_open: total}}, (e, r) ->
      if e
        console.log e
    )

当我调用"addMatching"时,会在"transactions"集合的"matchings"数组中添加一个对象。

在添加这个对象之后,我想重新计算事务的匹配总数,并使用"updateAmount"方法进行更新。

我不知道为什么,但"updateAmount"似乎在"matchings"更新结束之前运行。

不过它在回调中。

这是与延迟补偿有关的问题吗?我必须把这些方法放在服务器端吗?或者有解决方案吗?

当您从集合(调用cursor.fetchcollection.findOne)中获取文档时,您会得到一个表示该文档的JavaScript对象。该JavaScript对象不会受到集合中文档更新的影响。所以不是:

Meteor.call "updateAmountOpen", transaction

你需要再次获取它,这样你就可以获得最新更新的字段:

Meteor.call "updateAmountOpen", Transaction.findOne(transaction._id)

最新更新