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

时间:2015-03-31 05:42:42

标签: mongodb asynchronous meteor

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

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
    )

当我打电话给#34; addMatching"时,会在"匹配"中添加一个对象。 "交易的数组"集合。

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

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

虽然它在回调中。

这是与延迟补偿有关的问题吗?我是否必须将这些方法置于服务器端,或者是否存在解决方案?

1 个答案:

答案 0 :(得分:1)

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

Meteor.call "updateAmountOpen", transaction

您需要再次获取它,以便更新最新的字段:

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