2013-04-10 76 views
3

如何獲取我在集合或方法中的失敗插入中引發的錯誤的值。如何在流星視圖模板中顯示錯誤

Customers.allow({ 
insert: function(userID, rec) { 
    console.log(userID === rec.userID); 

    if (rec.userID === null) { 
     throw new Meteor.Error(600, "You must be logged in"); 
    }; 

    if (rec.phone.length != 10) { 
     throw new Meteor.Error(601, "Incorect phone format", "Phone must be 10 chars long"); 
    }; 

    if (rec.fax.length != 10) { 
     throw new Meteor.Error(602, "Incorect fax format", "Fax must be 10 chars long"); 
    }; 

    return userID === rec.userID; 
} 

});

所以現在我看到控制檯上的錯誤,但是說如果想要顯示模板中的錯誤或將其存儲在反應會話中,以便它可以顯示給用戶進行更正。

像嘗試到這樣的事情。

Template.form.errors = function() { 
    // return however you get to those thrown errors 
} 
+0

你有沒有管理,使這項工作? Tarang提供的解決方案不適用於我。謝謝。 – Ben 2013-11-17 02:58:22

+0

ok對我而言,現在我在Tarang的答案中添加了一條評論,它使它適用於我 – Ben 2013-11-17 21:02:30

回答

2

有今天剛剛發佈了一個包,以幫助這個:https://github.com/tmeasday/meteor-errors

您需要的隕石使用它:https://github.com/oortcloud/meteorite

添加包隕石:

mrt add errors 

(不要擔心,除了命令之外,你不會向你的流星增加錯誤;)

然後,您可以拋出錯誤在客戶端JS:

Meteor.Errors.throw("Error details"); 

那麼無論你想顯示的錯誤使用,在你的HTML:

{{>meteorErrors}} 
+0

對我來說,Meteors.Errors.throw(「錯誤細節」)不起作用,但Errors.throw(「錯誤細節」)做到了。當你想把錯誤返回給客戶端時,以下工作:Meteor.call('yourMethod',yourOptions,function(err){if(err)Errors.throw(err.reason);}); – Ben 2013-11-17 21:04:59