UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下在异步函数内部抛出

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of async function without a catch block

我 运行 mongo-express 实例在我的本地 docker 集线器容器上使用 kubectl,当我开始部署时,我在 Mongo 中遇到异常-快递实例。 下面是我的 mongo-express.yaml 文件。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-express
  labels:
    app: mongo-express
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-express
  template:
    metadata:
      labels:
        app: mongo-express
    spec:
      containers:
      - name: mongo-express
        image: mongo-express
        ports:
        - containerPort: 8081
        env:
          - name: ME_CONFIG_MONGODB_ADMINUSERNAME
            valueFrom:
              secretKeyRef: 
                name: mongodb-secret
                key: mongo-root-username  
          - name: ME_CONFIG_MONGODB_ADMINPASSWORD
            valueFrom:
              secretKeyRef: 
                name: mongodb-secret
                key: mongo-root-password 
          - name: ME_CONFIG_MONGODB_SERVER
            valueFrom:
              configMapKeyRef: 
                name: mongodb-configmap
                key: database_url  

下面是我的mongo-deployment.yaml文件,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef: 
              name: mongodb-secret
              key: mongo-root-username 
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef: 
              name: mongodb-secret
              key: mongo-root-password 
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports: 
    - protocol: TCP
      port: 80
      targetPort: 27017 

同样我也有configmap和secret文件 但是,当我应用这两个文件时,我在 docker container,

上遇到异常
Welcome to mongo-express
------------------------



(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Could not connect to database using connectionString: mongodb://root:root@mongodb-service:27017/"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb-service:27017] on first connect [MongoNetworkTimeoutError: connection timed out
    at connectionFailureError (/node_modules/mongodb/lib/core/connection/connect.js:342:14)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:310:16)
    at Object.onceWrapper (events.js:420:28)
    at Socket.emit (events.js:314:20)
    at Socket._onTimeout (net.js:483:8)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:314:20)
    at /node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /node_modules/mongodb/lib/core/connection/pool.js:995:11
    at /node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:280:5)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:310:7)
    at Object.onceWrapper (events.js:420:28)
    at Socket.emit (events.js:314:20)
    at Socket._onTimeout (net.js:483:8)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7) [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.

我不确定为什么会收到此错误。谁能建议该怎么做?

如果mongo-express yaml 文件中有一个选项,请尝试添加一个依赖变量。在 docker 中,当我在 mongo-express.

的 yaml 中添加 depends_on 时它起作用
depends_on:
      - mongodb-service

mongodb-service 我认为是您在 mongodb yaml 中使用的服务的名称。

欢迎来到社区。

您为 mongo db 提供的服务有一个小错误。监听端口应该是 27017 而不是 80:

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports: 
    - protocol: TCP
      port: 27017
      targetPort: 27017

这允许我的 mongo express 连接到数据库:

kubectl logs mongo-express-yyyyyyyyy-xxxxx
Welcome to mongo-express
------------------------


(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!