我使用throw new Meteor.Error
從服務器方法拋出錯誤。如何從服務器端拋出錯誤並從客戶端捕獲錯誤?
客戶端似乎沒有拿起它,並且回調的error
參數始終爲undefined
,但在終端中它出現錯誤並重新啓動Meteor。
W20170413-17:27:28.900(1)? (STDERR) /home/xeconcepts/.meteor/packages/meteor-tool/.1.4.2-1-beta.1.si3hb0++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280 W20170413-17:27:28.900(1)? (STDERR) throw(ex); W20170413-17:27:28.900(1)? (STDERR) ^ W20170413-17:27:29.175(1)? (STDERR) Error: carte existe [204] W20170413-17:27:29.175(1)? (STDERR) at imports/api/users/methods.js:211:47
和條紋methods
或
if(Meteor.isServer)
如何從服務器端拋出一個錯誤,並從客戶端捕獲它
const Stripe = require('stripe');
const stripe = Stripe("sk_test_pO*******MUDXjlF8v");
將無法正常工作?
方法代碼:
export const addCCStudent = new ValidatedMethod({
name: 'addCCStudent',
validate: new SimpleSchema({
id: {
type: String
},
number: {
type: String
},
dateMM: {
type: String
},
dateYYYY: {
type: String
},
cvc: {
type: String
}
}).validator(),
run(p) {
try {
if (Meteor.isServer) {
const Fiber = require('fibers');
const Stripe = require('stripe');
const stripe = Stripe("*************");
stripe.tokens.create({
card: {
"number": p.number,
"exp_month": p.dateMM,
"exp_year": p.dateYYYY,
"cvc": p.cvc
}
}, function(err, token) {
console.log("tokenserr", err);
console.log("token", token);
if (!err) {
Fiber(function() {
// console.log("p", p)
var user = Meteor.users.findOne({
_id: p.id
});
//console.log("addCCStudentuser", user);
if (user) {
var cCArray = user.profile.UserCards;
if (cCArray) {
var exist = false;
for (var i = 0; i < cCArray.length; i++) {
if ((cCArray[i].number == p.number) && (cCArray[i].dateMM == p.dateMM) && (cCArray[i].dateYYYY == p.dateYYYY)) {
exist = true
}
}
if (exist) {
throw new Meteor.Error(204, "carte existe");
} else {
Meteor.users.update({
_id: p.id,
}, {
$push: {
'profile.UserCards': { number: p.number, dateMM: p.dateMM, dateYYYY: p.dateYYYY }
}
});
}
}
}
}).run();
} else {
// console.log("errerrerrerr", err.raw.message);
throw new Meteor.Error(204, err.raw.message);
}
});
}
} catch (error) {
console.log("error", error)
throw new Meteor.Error(203, error.reason);
}
},
});
我可以看到您的完整代碼嗎?你是如何發現錯誤 – Ethaan
如果你不發佈你的代碼 – Sean
https://pastebin.com/G0m1kDAq –