Node.js 更新节点后传递空值时出错

Node.js 更新节点后传递空值时出错,node.js,express,mongoose,Node.js,Express,Mongoose,我遇到了一个问题,我调用了Node/Express/Mongo(mongoose)API,在这里,如果我传递的参数为null,我会得到一个强制转换错误 突破点是这个调用: User.findOne({ _id: ownerId }).then((data) => { 如果ownerId为“null”,则会出现以下错误: (node:4186) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection

我遇到了一个问题,我调用了Node/Express/Mongo(mongoose)API,在这里,如果我传递的参数为null,我会得到一个强制转换错误

突破点是这个调用:

User.findOne({ _id: ownerId }).then((data) => {
如果ownerId为“null”,则会出现以下错误:

(node:4186) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value "" at path "_id" for model "User"
(node:4186) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
我最近从node 6.9.5更新到了node的最新版本

不知道该怎么处理


有什么帮助吗?

您会收到这个错误,因为
ownerId
是一个空字符串,它必须是一个有效的ObjectId,类似于
577FC3002ECC12D154631E1
,所以下面是要做的:

if(mongoose.Types.ObjectId.isValid(ownerId)){
    return User.findOne({ _id: ownerId }).then((data) => { ... })
} else{
   return Promise.resolve(null);
}