gpt4 book ai didi

meteor - 如何在 meteor View 模板中显示错误

转载 作者:行者123 更新时间:2023-12-02 02:04:04 26 4
gpt4 key购买 nike

如何获取在集合或方法中插入失败时抛出的错误的值。

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;
}

});

所以现在我在控制台上看到了错误,但说如果想要在模板中显示错误或将其存储在 react 性 session 中,以便可以向用户显示以进行更正。

喜欢尝试这样的事情。

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

最佳答案

今天刚刚发布了一个包来帮助解决这个问题:https://github.com/tmeasday/meteor-errors

你需要陨石才能使用它:https://github.com/oortcloud/meteorite

添加陨石包:

mrt add errors

(别担心,除了命令之外,您不会向 meteor 添加错误;)

然后您可以在客户端 js 中抛出错误:

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

然后在任何你想显示错误的地方使用,在你的 HTML 中:

{{>meteorErrors}}

关于meteor - 如何在 meteor View 模板中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15936000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com