使用 SimpleSchema 在 Meteor.js 中处理错误
Error handling in Meteor.js with SimpleSchema
我正在做一个由 Meteor.js 开发的项目,现在我正在做类似的验证工作
import SimpleSchema from 'simpl-schema';
const CompanySchema = new SimpleSchema({
company_name: {
type: String,
min: 5,
max: 50,
label: "Company Name"
}
});
Company.attachSchema(CompanySchema);
但在控制台中显示如下图
但是当试图以这种方式保持 "Error" 时
console.log(err.Error);
正在显示
undefined
这里是插入功能
Company.insert(
{
company_name: inputs.companyName.value,
},
function(err) {
if (err) {
console.log(err);
} else {
console.log('Inserted successfully');
}
}
);
实际上是什么问题。
谢谢
当您的客户端 Mongo 插入失败时,它会生成 native Error
。如果您记录它是 name
、message
和 stack
,它会显示 Error
:
的预期属性
Company.insert(
{
company_name: inputs.companyName.value,
},
function(err) {
if (err) {
console.log(err.name);
console.log(err.message);
console.log(err.stack);
}
}
);
产生:
Error
Company Name must be at least 5 characters in company insert
Error: Company Name must be at least 5 characters in company insert
at getErrorObject (collection2.js:498)
at doValidate (collection2.js:470)
at Collection.Mongo.Collection.(:3000/anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2.js?hash=9ed657993899f5a7b4df81355fd11d6b77396b85:286:14)
at Blaze.TemplateInstance.helloOnCreated (main.js:10)
at blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3398
at Function.Template._withTemplateInstanceFunc (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3769)
at fireCallbacks (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3394)
at Blaze.View.<anonymous> (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3474)
at fireCallbacks (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:2014)
at Object.Tracker.nonreactive (tracker.js:603)
相比之下,属性 err.error
是 the Meteor.Error
, which is thrown if the insert fails inside a Meteor Method 的一部分。
例如在这样的代码中就是这种情况:
Meteor.call('someInserMethod', { company_name: 'abc' }, (err, res) => {
console.log(err) // this error is a Meteor.Error
})
我正在做一个由 Meteor.js 开发的项目,现在我正在做类似的验证工作
import SimpleSchema from 'simpl-schema';
const CompanySchema = new SimpleSchema({
company_name: {
type: String,
min: 5,
max: 50,
label: "Company Name"
}
});
Company.attachSchema(CompanySchema);
但在控制台中显示如下图
但是当试图以这种方式保持 "Error" 时
console.log(err.Error);
正在显示
undefined
这里是插入功能
Company.insert(
{
company_name: inputs.companyName.value,
},
function(err) {
if (err) {
console.log(err);
} else {
console.log('Inserted successfully');
}
}
);
实际上是什么问题。
谢谢
当您的客户端 Mongo 插入失败时,它会生成 native Error
。如果您记录它是 name
、message
和 stack
,它会显示 Error
:
Company.insert(
{
company_name: inputs.companyName.value,
},
function(err) {
if (err) {
console.log(err.name);
console.log(err.message);
console.log(err.stack);
}
}
);
产生:
Error
Company Name must be at least 5 characters in company insert
Error: Company Name must be at least 5 characters in company insert
at getErrorObject (collection2.js:498)
at doValidate (collection2.js:470)
at Collection.Mongo.Collection.(:3000/anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2.js?hash=9ed657993899f5a7b4df81355fd11d6b77396b85:286:14)
at Blaze.TemplateInstance.helloOnCreated (main.js:10)
at blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3398
at Function.Template._withTemplateInstanceFunc (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3769)
at fireCallbacks (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3394)
at Blaze.View.<anonymous> (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:3474)
at fireCallbacks (blaze.js?hash=51f4a3bdae106610ee48d8eff291f3628713d847:2014)
at Object.Tracker.nonreactive (tracker.js:603)
相比之下,属性 err.error
是 the Meteor.Error
, which is thrown if the insert fails inside a Meteor Method 的一部分。
例如在这样的代码中就是这种情况:
Meteor.call('someInserMethod', { company_name: 'abc' }, (err, res) => {
console.log(err) // this error is a Meteor.Error
})