2014-04-13 38 views
1

我正在使用Mongoose(v。3.8.8)更新我的模型“权限”的一些数据。下面是模式:使用Promises处理CastoError与猫鼬

var PermissionSchema = new Schema({ 

     name : { 
      type: String, required: true, index: true 
     }, 
     httpVerb : { 
      type: String, required: true 
     }, 
     url : { 
      type: String, required: true 
     } 
}); 

mongoose.model('Permission', PermissionSchema); 

现在我使用的承诺在另一模块功能:

module.exports.updatePermission = function (id, updatedPermission) { 

    var conditions = {_id: id}; 

    var promiseUpdate = Permission.update(conditions, updatedPermission).exec(); 

    logger.debug('this line is not executed when CastError occurs'); 

    var promiseFind = promiseUpdate.then(function (permission) { 
     return Permission.findOne(conditions).exec(); 
    }).then(null, function (error) { 
      logger.info('CastError does not get in here'); 
      logger.error(error); 
      //error handled here 
     }); 

    return promiseFind; 
} 

当我打电话与这个例子参数功能:

id: 53496a2e1722ff2d1a55a4d2 
updatedPermission: {name: 'this is my new name'} 

一切正常罚款因为_id 确实已经存在于集合中。

现在,当我用传递的目的下列参数的无效和不存在的_id:

id: 53496a2e1722ff2d1a55a4d22 
updatedPermission: {name: 'this is my new name'} 

我得到以下异常:

CastError: Cast to ObjectId failed for value "53496a2e1722ff2d1a55a4d22" at path "_id" 
    at ObjectId.cast (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/schema/objectid.js:116:13) 
    at ObjectId.castForQuery (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/schema/objectid.js:165:17) 
    at Query.cast (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/query.js:2291:32) 
    at castQuery (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/query.js:2015:18) 
    at Query.update (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/query.js:1704:21) 
    at Function.update (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/node_modules/mongoose/lib/model.js:1606:13) 
    at Object.module.exports.updatePermission (/home/jhon/environment/workspaces/nodejs-workspace/auth/server/app/repository/auth/PermissionRepository.js:50:36) 

我期待得到这个错误当承诺被拒绝时,也就是updatePermission函数的第二个“then”。

现在的问题是:这是一个猫鼬的错误? - 如何在不使用try/catch的情况下处理这个问题?

回答

0

刚刚发现如何在这种特殊情况下使用promise。猫鼬有一个函数findOneAndUpdate(http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate),它按预期工作。 CastError通过拒绝。这样我就可以在没有try/catch块的情况下处理错误。

这是代码(PermissionRepository.js):

module.exports.updatePermission = function (id, updatedPermission) { 
    var conditions = {_id: id}; 

    var promiseUpdate = Permission.findOneAndUpdate(conditions, updatedPermission).exec(); 

    return promiseUpdate; 
} 

下面的代码调用以前的一个(CastError在第二个 “然后” 处理):

module.exports.updatePermission = function (req, res) { 
    var id = req.query.id; 
    var updatedPermission = req.body; 

    var permissionRepository = repositoryFactory.getPermissionRepository(); 
    var promise = permissionRepository.updatePermission(id, updatedPermission); 

    promise.then(function (permission) { 
     if (!permission) { 
      res.status(404).json(Error.resourceNotFound); 
     } else { 
      res.status(200).json(permission); 
     } 
    }).then(null, function (error) { 
      logger.error(error.stack); 
      res.status(500).json(Error.databaseError(error.message, error)); 
     }); 
} 

现在的我m很确定方法更新中有一个错误(http://mongoosejs.com/docs/api.html#model_Model.update)。我会报告它。