[英]How do I pass error messages from meteor server to client
我正在使用meteor方法从客户端函数中检索值。 我使用条纹api,它工作正常
但是当stripe.charges.create
函数中存在错误时,错误不会传递回客户端,这会导致stripeCheckout
方法将用户发送到完整模板。 我假设有一种方法可以从条带服务器Stripe.charges.create
err.type
响应中的错误,或者服务器端的Stripe.customers.create
函数将它们传递给客户端并让用户知道特定的错误以及不使用基于错误的if语句或传递给流星的条带服务器的成功状态将它们发送到完整模板。
这是从条带'Stripe.charges.create function to the meteor server and then passing it back to the client through the
的错误响应的连接function to the meteor server and then passing it back to the client through the
stripeCheckout`方法function to the meteor server and then passing it back to the client through the
。
好的希望我能解决这个问题。 任何提示,以实现这种令牌创建,客户创建和更好的实践收费,我愿意接受任何建议。
谢谢!
客户端JS
Stripe.card.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val(),
address_line1: addressValue1,
address_line2 : addressValue2,
address_city: cityValue,
address_state: provinceState,
address_country: country,
address_zip: zip,
name: firstName + lastName
}, stripeResponseHandler);
function stripeResponseHandler(status, response) {
if (response.error) {
alert(response.error.message);
}else{
// response contains id and card, which contains additional card details
var token = response.id;
Meteor.call('stripeCheckout',token, function (error, result) {
Router.go("/complete");
});
}
}
服务器JS
Meteor.methods({
stripeCheckout: function (token) {
Stripe.customers.create({
source: token
}, function(err, customer) {
id = customer.id;
if(err){
throw new Meteor.Error("create-failed", err.message);
}else{
throw new Meteor.Error("create-failed", err.message);
}
Stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: id
}, function (err, res) {
if(err){
throw new Meteor.Error("create-failed", err.message);
}else{
throw new Meteor.Error("create-failed", err.message);
}
});
});
}
});
更新:
我添加了当前的错误检测,我在所有实例中抛出一个错误,并在控制台中收到此响应。
Exception in delivering result of invoking 'stripeCheckout': TypeError: Cannot read property 'error' of undefined
at http://localhost:3000/client/stripeClient.js?0eb126fd5e018d3cac3f8ec1505f32b7fdc97604:197:22
at Meteor.bindEnvironment [as _callback] (http://localhost:3000/packages/meteor.js?81e2f06cff198adaa81b3bc09fc4f3728b7370ec:977:22)
at _.extend._maybeInvokeCallback (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:3858:12)
at _.extend.receiveResult (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:3878:10)
at _.extend._livedata_result (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:4931:9)
at onMessage (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:3723:12)
at http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:2717:11
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:11)
at _.extend._launchConnection.self.socket.onmessage (http://localhost:3000/packages/ddp.js?41b62dcceb3ce0de6ca79c6aed088cccde6a44d8:2716:11)
这是Stripe POST /v1/customers
响应
{
error:{
message: "Your card was declined."
type: "card_error"
code: "card_declined"
}
}
简单地抛出一个像这样的Meteor.Error
:
Meteor.methods({
stripeCheckout: function (token) {
Stripe.customers.create({
source: token
}, function(err, customer) {
id = customer.id;
Stripe.charges.create({
amount: 1000,
currency: 'usd',
customer: id
}, function (err, res) {
// first argument is error code, second is error details
throw new Meteor.Error("create-failed", err.message);
});
});
}
});
您将收到您在方法回调的error
参数中抛出的error
。
请参阅此处的文档: http : //docs.meteor.com/#/full/meteor_error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.