【问题标题】:Unable to retrieve _id after insert in a Meteor.method call插入 Meteor.method 调用后无法检索 _id
【发布时间】:2016-04-15 02:45:15
【问题描述】:

我无法使用我在同一个问题上找到的答案来获取插入的 _id 的返回值;

Meteor.call('saveDocument', value1, value2, function(error, result){
 var theIdYouWant = result;
});

在服务器上;

saveDocument: function (value1, value2) {
  return MyCollection.insert({ 'value1': value1, 'valu2': value2}); 
}

答案来自 2013 年 5 月,所以我不知道这是否是正确的方法错误和结果始终未定义。但是,我可以通过将其保存在这样的服务器上来使其工作;

    'transactionInsert': function(currentUserId, trx_num, other_stuff){
        trxId = trx.insert ({
            "userID": currentUserId,
            "trx_num": trx_num, 
            ... other stuff ...
        });
    },
    'insertInvoiceItem': function(currentUserId, invoiceNum){
        invoiceItems.insert(
            {"userID": currentUserId,
            "invoice_num": invoiceNum,
            "trx_id": trxId}            
        );
    }

transactionInsert 之后立即从客户端调用insertInvoiceItem。我的问题是trxId 是否始终是交易的正确 id,即使成千上万的用户正在使用它,还是其他用户的交易会导致 id 不正确?

更新:

我错了,还是不行。这就是我现在的位置;

Meteor.call('transactionInsert', Meteor.userId(), trx_num, 
    function(error, result){
        console.log(Session.get("trxId")); // p2YrLTiypiMm4tKma appears AFTER undefined in the console
        Session.set("trxId",result);
    });
console.log("trxId is " + Session.get("trxId")); // undefined
Meteor.call('insertInvoiceItem', Meteor.userId(), invoiceNum, Session.get("trxId")

Template.writeInvoice.helpers({  
    trxId: function() {
        return Session.get("trxId"); 
    }
})

在服务器上;

Meteor.methods({
    'transactionInsert': function(currentUserId, trx_num){
        return trx.insert ({
            "userID": currentUserId,
            "trx_num": trx_num
        })
    },
    'insertInvoiceItem': function(currentUserId, invoiceNum, trxId){
        invoiceItems.insert(
            {"userID": currentUserId,
            "invoice_num": invoiceNum,
            "trx_id": trxId}
        )
    }
})

【问题讨论】:

  • 您的原始代码对我来说看起来不错。如果你 console.log(result) inside 回调会发生什么?我怀疑您遇到的问题是 theIdYouWant 未在回调的外部定义。这是意料之中的,因为它是一个异步函数。请参阅this 示例。
  • 奇怪。该值显示在控制台中,而变量仍未定义。所以如果我在模板中使用theIdYouWant 那么它应该可以工作吗?像我现在这样在服务器上将其定义为全局变量有什么问题吗?

标签: mongodb meteor


【解决方案1】:

正如 David Weldon 所说,这可能只是范围问题。您的变量在回调之外不存在。尝试像这样定义它:

var id;

Meteor.call('saveDocument', value1, value2, function(error, result){
 id = result;
});

//Do stuff with the id...

或者,如果您在反应式计算(如模板)中需要它:

var id = new ReactiveVar(); //meteor add reactive-var

Meteor.call('saveDocument', value1, value2, function(error, result){
 id.set(result);
});

//Use id.get() where you need it

【讨论】:

  • 两种方法都试过了,结果还是不确定。我可以在控制台中得到它,但它仍然不是异步的。
  • 更新后我真的不明白你的代码发生了什么。你说你的console.log(Session.get("trxId")); 在控制台中显示了一些东西,但是从我看到的第一个Session.set() 发生在之后......你能给我们一个 MeteorPad 或更精确的控制台日志吗?编辑:哦,等等我明白了。好的。我明白你的意思,但我看不出你的代码有什么问题......如果你还没有找到任何解决方案,请尝试做一个 MeteorPad 让我们看到代码在运行中。
  • 我在这里提供了足够的代码来复制问题 - meteorpad.com/pad/RCTD7gvhDwscZebrS/Leaderboard
  • 在你的meteorpad(client.js)中,你每次输入帮助器时都会初始化trxId,所以在meteor方法调用回调之外,在回调运行之前它总是一个空字符串。我正在制作另一个垫子......编辑:This works。我添加了一个助手并改变了trxId 的处理方式。告诉我这是否是您搜索的内容,如果是,我将编辑我的答案。
  • 就是这样!这与我尝试过的事情之一非常接近,但是在设置了帮助器之后,我从未真正使用过帮助器,因为我在其他任何地方都不需要该值。因此,如果我只是将 {{getLastTrxId}} 放在隐藏输入或非显示 div 中,那么这应该是缺少的链接。
猜你喜欢
  • 2013-05-02
  • 2021-01-23
  • 2014-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多