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

meteor update callback shortcut by latency compensation?

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

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)