2017-11-26 3 views
0

노드/익스프레스/몽고 (몽구스) API에 대한 호출이있는 문제가 있습니다. 한 시점에서 매개 변수가 null 인 경우 null이 캐스트가됩니다. 오류.노드 업데이트 후 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 

내가 최근에 최신에 노드 6.9.5에서 업데이트 한을 노드의 버전.

처리 방법을 잘 모르겠습니다.

어떤 도움이 필요합니까? ownerId은 빈 문자열 때문에

답변

1

당신이 오류가있어, 나해야 유효한 ObjectId가이 577fc3002eccc12d154631e1 같은, 그래서 여기에 수행 할 작업은 다음과 같습니다

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