有下面的代碼:catch異常/ promisses
router.put('/:id', function(req, res, next) {
models.Task.find(req.params.id).then(function(task) {
return task.updateAttributes({
title: req.body.title,
description: req.body.description
});
}).then(function(task) {
res.json(task);
}).catch(function(err) {
var err = new Error('Cannot find Task');
err.status = 404;
next(err);
});
});
正如你所看到的,我用這條路線由id來更新任務模式。所以,現在我趕上錯誤,如果沒有任何與id的任務,我嘗試更新null對象的屬性(ReferenceError錯誤類型)。所以,我剛剛向任務模型添加了驗證,並且有機會使用空標題/描述(SequelizeValidationError錯誤類型)更新任務。我如何在我的catch塊中處理這些錯誤?我想設置'無法找到任務','無法更新任務'消息這個錯誤。我應該分裂我的catch塊還是其他什麼?提前致謝!
身份證建議使用藍鳥承諾,而不是看到這個答案爲什麼http://programmers.stackexchange.com/questions/278778/why-are-native-es6-promises-slower-and-more-memory-intensive-than -bluebird – baao