- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Mongoose(v. 3.8.8)来更新我的模型“Permission”的一些数据。这是架构:
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);
现在我有一个在另一个模块中使用 Promise 的函数:
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)
我预计当 Promise 被拒绝时,即在 updatePermission 函数的第二个“then”中,会出现此错误。
问题是:这是 Mongoose 的 bug 吗? - 在不使用 try/catch 的情况下如何处理这个问题?
最佳答案
刚刚找到了在这种特殊情况下如何使用 promise 来做到这一点。 Mongoose 有一个函数 findOneAndUpdate (http://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate),它按预期工作。 CastError 通过拒绝传递。这样我就可以在没有 try/catch block 的情况下处理错误。
这是代码(PermissionRepository.js):
module.exports.updatePermission = function (id, updatedPermission) {
var conditions = {_id: id};
var promiseUpdate = Permission.findOneAndUpdate(conditions, updatedPermission).exec();
return promiseUpdate;
}
以下代码调用前一个代码(CastError 在第二个“then”中处理):
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));
});
}
现在我非常确定方法更新中存在错误( http://mongoosejs.com/docs/api.html#model_Model.update )。我会举报的。
关于node.js - 使用 Promises 处理 Mongoose 的 CastError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23038557/
我收到这个错误 { [CastError: Cast to Number failed for value "NaN" at path "area"] message: 'Cast t
我有一个具有此功能的 excel 插件类: public void testRibbon() { Excel.Workbook workbook = this.Application.Act
我有以下包含嵌套模式的数据: 用户模式 (function userModel() { var mongoose = require('mongoose'); var Entry = requi
我有以下架构 var Topic= new Schema({ text: String, topicId: String, comments: [{type: Schema.Types.O
我有两个模式:架构一(父级): var slide = require('../model/slide'); var slidesetSchema = new Schema({ name: S
我正在尝试使用 Express 和 Mongoose 创建一个简单的表单,允许用户将新项目添加到集合中。 Mongoose 模式: var Item = new Schema({ name :
我想要实现的目标:在另一个架构(配方架构)中使用一个架构(成分架构)。我的目标是在食谱模式中,我的配料键是我的配料模式的数组。不知道为什么它不起作用或我做错了什么。如果你们有任何想法,我将非常感激。请
当我尝试将数据保存在数据库中时,它给出禁止消息 CastError:转换为未定义的值失败 [ {"product":{"supplierId":{"undefined":"rfytr"}}},
问题 当使用 mongoose 按特定字段查找文档时,如果字段的类型与查询值的类型不同,则 mongoose 将尝试将查询值转换为与字段相同的类型。 但是,如果无法转换该值,则 mongoose 将抛
我正在使用 Mongoose(v. 3.8.8)来更新我的模型“Permission”的一些数据。这是架构: var PermissionSchema = new Schema({ n
错误消息:"message": "Cast to ObjectId failed for value\"{ [Function: ObjectID]\n 我尝试将 id 变量类型转换为 ObjectI
基本上,我的 Node 服务器上有一些代码,如下所示: var blogId = req.params.blogId; Blog.findById(blogId).exec(...) 我收到此错
我有以下 Mongoose 架构: { lastEdit: Number translations: [{ submittedTimestamp: Number
我知道这个问题有很多版本,但我找不到任何对我有帮助的东西。 function isAuthenticated() { return compose() // Validate jwt
我正在尝试将地理点和地理多边形保存到 Mongo。我的点测试通过,但多边形失败: CastError: Cast to number failed for value "0,0,3,0,3,3,0,3
更新到 Mongoose 5.11.13 后,尝试将项目添加到文档内的子对象时出现以下错误。 CastError: Cast to embedded failed for value "{ value
我正在使用node / express / mongo / mongoose构建一个应用程序。我遇到了一个似乎无法弄清的错误,到目前为止,对Google进行谷歌搜索一直没有帮助。 我尝试创建一个编辑页
我有一个用 mongoose 定义的嵌套模式: //application.js var mongoose = require('mongoose'); var Schema = mongoose.S
我是 node.js 的新手,所以这可能是一个愚蠢的错误。我有一个像这样的 mongodb 模式: seller_schema = mongoose.Schema({ name:Stri
将 Mongoose.js 与 node.js 结合使用。 我有这个架构: var Photo = new Schema({ URL:String ,description:Stri
我是一名优秀的程序员,十分优秀!