[英]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.