简体   繁体   中英

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:

<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. You might want to alert the error and return anyway. Just bear in mind the call is asynchronous.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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