簡體   English   中英

貓鼬查詢返回錯誤,但錯誤是我要尋找的結果

[英]Mongoose query returns error, but error is the result I am looking for

在貓鼬中,當我調用以下代碼時:

db.Person.find({}).then((err, author) => {
   if (err) {
     console.log("err",err);
   } else {
    console.log('author', author);
    }
  });

它返回一個錯誤,而不是我要查找的文檔,並且該錯誤似乎是我要查找的文檔。 看這里: 在此處輸入圖片說明

為什么會返回錯誤而不是實際對象? 我試圖從返回的文檔訪問屬性。 該對象存在於我的數據庫中: 在此處輸入圖片說明

根據文檔: https ://mongoosejs.com/docs/promises.html您應該使用query.exec()獲得完整的承諾:

// `.exec()` gives you a fully-fledged promise
var promise = query.exec();

promise.then(function (doc) {
  // use doc
});

當您像承諾一樣使用它時,

.then((doc)=>console.log(doc))

doc是實際的文檔,最終會出現錯誤

.catch(err=>console.log(err))

當您使用.then()並且承諾被解決時,您將在then()得到結果。

.then((result) => console.log(result))

當承諾被拒絕時,您可以捕獲以下錯誤:

.catch((error) => console.log(error))

承諾分為三個階段: resolverejectpending

暫無
暫無

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

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