簡體   English   中英

流星模板助手未返回值

[英]Meteor template helper not returning value

我編寫了一種將值返回到客戶端的方法:

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>

為了測試我的代碼,我輸入了兩個警報。 第一個警報正確警報結果。 但是,第二個警報不執行任何操作。 有人可以幫我指出我做錯了什么嗎?

謝謝!

您正在返回else子句中的值,因此永遠不會調用另一個代碼段。 您可能要警告該錯誤並返回。 請記住,該調用是異步的。

您將在第一個alert之后返回結果,該alert將停止在function內進一步執行代碼,因此第二個alert不會運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM