簡體   English   中英

Mongoose findOne將文檔作為錯誤返回?

[英]Mongoose findOne is returning the doc as the error?

在我的智慧結束。 似乎無法弄清楚為什么會這樣。

Trade.findOne( { ticker } ).then( ( err, doc ) => {
  if ( err ) {
    console.log( 'THERE IS AN ERROR:', '\n', err )
  } else {
    console.log( 'no error' )
    if ( doc ) {
      console.log( 'doc', doc )
    } else {
      console.log( 'no doc' )
    }
  }
} )

當這段代碼運行時,它進入錯誤情況和控制台日志,這THERE IS AN ERROR ,當我注銷err ,它會注銷找到的正確文件。 我似乎無法弄清楚為什么會這樣。 是否有更好的錯誤消息?

我想也許有些東西與我的架構有沖突但我一行一行地通過數據仍然沒有。

編輯:

太奇怪了......我把我的代碼結構改為:

Trade.findOne( { ticker } ).then( doc => {
  if ( doc ) {
    console.log( 'doc', doc )
  } else {
    console.log( 'no doc' )
  }
} ).catch( err => {
  console.log( 'err', err )
} )

這有效...為什么?

Mongoose的回調提供錯誤作為第一個參數,結果作為第二個參數。

使用promises時,錯誤會像上面一樣在catch處理程序中捕獲 - 並且結果通過then處理程序進行級聯。

看一下:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM