简体   繁体   English

流星模板助手未返回值

[英]Meteor template helper not returning value

I wrote a method to return a value onto the client: 我编写了一种将值返回到客户端的方法:

Template.Cart.helpers({

  cartPrice: function(result) {
    Meteor.call('returnCartPrice', function(error, result) {
    if(error) {
      alert(error.reason);
    } else {
      alert('result is ' + result); //this alerts
      return result;
    }
    alert('result is ' + result); //this does NOT alert
    return result;
  });
 }
});

cart.html: cart.html:

<p>{{cartPrice}}</p>

To test my code, i put in two alerts. 为了测试我的代码,我输入了两个警报。 The first alert correctly alerts the result. 第一个警报正确警报结果。 However, the second alert does not do anything. 但是,第二个警报不执行任何操作。 Can someone help point out what I am doing wrong? 有人可以帮我指出我做错了什么吗?

Thank you! 谢谢!

You are returning the value inside the else clause, thus the other snippet is never called. 您正在返回else子句中的值,因此永远不会调用另一个代码段。 You might want to alert the error and return anyway. 您可能要警告该错误并返回。 Just bear in mind the call is asynchronous. 请记住,该调用是异步的。

您将在第一个alert之后返回结果,该alert将停止在function内进一步执行代码,因此第二个alert不会运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM