0
考慮以下代碼:在try-catch塊中,是否會出現mongoose.save()拋出錯誤對象的問題?
mongoose.connect('MyDatabaseURL');
var sch_obj = {field1: String};
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
var model_obj = db.model('SchemaName', sch_obj);
var obj = new model_obj({field1:'MyValue'});
obj.save(function(err,data){
if(err)
console.log('error occurred:' + err); // <=== Case 1
else
console.log('saved');
});
});
/* ---------------------------------- */
mongoose.connect('MyDatabaseURL');
var sch_obj = {field1: String};
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
var model_obj = db.model('SchemaName', sch_obj);
var obj = new model_obj({field1:'MyValue'});
obj.save(function(err,data){
try {
console.log('saved');
}
catch(err) // <=== Case 2
{
console.log('error occurred:' + err);
}
});
});
問題:他們是一樣的嗎?如果是,哪一個是處理錯誤的好方法?
任何支持文檔鏈接都將有很大的幫助..感謝您的快速回答... – 2013-02-14 10:15:46
@AmolMKulkarni,請參閱[Mongoose Docs](http://mongoosejs.com/docs/models.html)瞭解更多適當的例子錯誤處理。 – 2013-02-14 10:18:18