2017-11-26 16 views
0

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 

私は最近、最新のノード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); 
} 
関連する問題