我如何确保在调用函数之前实现承诺?

问题描述 投票:-1回答:1

我试图确保在将客户端发送到我创建的一个模块之前,我得到一个正确的MongoDB连接。我对这个服务器的目标是让它接收GET方法,运行issue.getIssues函数,然后该函数请求我的MongoDB服务器中的所有文档,然后接收这些文档,然后将其发送回原始函数,然后该函数将值提供给我的模板,该模板应该然后显示所有文档。我的错误是。

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.resolution() 强制解决承诺问题 但我就是想不通。这可能只是我不了解异步编程,但我需要帮助。我的代码可以在这个Repl.it上找到。https:/repl.itreplsLazyOldCommercialsoftware。我无法让repl识别本地文件,以及明显的MongoDB不能工作了,但你可以戳穿我的代码。

javascript node.js express asynchronous pug
1个回答
0
投票

你是在传递一个Promise到 getIssues's client 参数,而不是MongoDB客户端,这就是为什么错误的原因。client.db 不是一个函数。或者你可以删除 Promise.resolve 中的路由issues.js或使用 (await client).db 在issue-managerissues.js中。

© www.soinside.com 2019 - 2024. All rights reserved.