簡體   English   中英

在調用 function 之前,如何確保滿足 promise 的要求?

[英]How can I ensure that a promise is fulfilled before calling a function?

在將該客戶端發送到我創建的模塊之前,我試圖確保與 MongoDB 建立正確的連接。 我對該服務器的目標是讓它接收 GET 方法,運行 issue.getIssues function,然后請求我的 MongoDB 服務器中的所有文檔,然后接收這些文檔,然后將其發送回原始值 ZC1C425Z048E68394F14,然后給出到我的模板,該模板應該顯示所有文檔。 我的錯誤是:

UnhandledPromiseRejectionWarning: TypeError: client.db is not a function
    at Object.getIssues (...\issue_backend\node_modules\issue-manager\issue.js:6:35)
    at ...\issue_backend\routes\issues.js:14:30
    at Layer.handle [as handle_request] (...\issue_backend\node_modules\express\lib\router\layer.js:95:5)
    at next (...\issue_backend\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (...\issue_backend\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (...\issue_backend\node_modules\express\lib\router\layer.js:95:5)
    at ...\issue_backend\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (...\issue_backend\node_modules\express\lib\router\index.js:335:12)
    at next (...\issue_backend\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (...\issue_backend\node_modules\express\lib\router\index.js:174:3)

我嘗試過使用 await 和 promise.resolve() 強制解決 promise,但我就是想不通。 可能只是我不了解異步編程,但我需要幫助。 我的代碼可以在這個 Repl.it 上找到: https://repl.it/repls/LazyOldCommercialsoftware我無法讓 repl 識別本地文件,以及明顯的 MongoDB 不再工作,但你可以戳我的代碼.

您將 Promise 傳遞給getIssuesclient參數,而不是 MongoDB 客戶端,這就是錯誤顯示client.db不是 function 的原因。 您可以刪除 routes/ Promise.resolve中的 Promise.resolve 或使用 issue-manager/issues.js 中的(await client).db

暫無
暫無

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

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