【问题标题】:(node:16448) UnhandledPromiseRejectionWarning: MongoExpiredSessionError: Cannot use a session that has ended(节点:16448)UnhandledPromiseRejectionWarning:MongoExpiredSessionError:无法使用已结束的会话
【发布时间】:2021-12-11 06:50:51
【问题描述】:

我尝试从 Node 连接到 MongoDB,但出现此错误:

(使用node --trace-warnings ... 显示警告的创建位置) (节点:16448)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (节点:16448)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

这是我的文件。当然不用用户名和密码也没关系。

app.js

const express = require('express');
const bodyParser = require('body-parser')
const mongoPractice =require('./mongo')

const app = express();

app.use(bodyParser.json());

app.post('/products',mongoPractice.createProduct);

app.get('/products');

app.listen(3000);

mongo.js

const MongoClient = require("mongodb").MongoClient;

const url ='xxxxxx'

const createProduct = async (req, res, next) => {
    const newProduct = {
      name: req.body.name,
      price: req.body.price
    };
    const client = new MongoClient(url);
  
    try {
      await client.connect();
      const db = client.db();
      const result = db.collection('products').insertOne(newProduct);
    } catch (error) {
      return res.json({message: 'Could not store data.'});
    };
    client.close();
  
    res.json(newProduct);
  };
  
  const getProducts = async (req, res, next) => {
    const client = new MongoClient(url);
  
    let products;
  
    try {
      await client.connect();
      const db = client.db();
      products = await db.collection('products').find().toArray();
    } catch (error) {
      return res.json({message: 'Could not retrieve products.'});
    };
    client.close();
  
    res.json(products);
  };
  
  exports.createProduct = createProduct;
  exports.getProducts = getProducts;

package.json

{
  "name": "refresh-start",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongodb": "^4.1.3",
    "nodemon": "^1.19.4"
  }
}

如果我尝试这个 package.json 就可以了,但它已被弃用:

{
  "name": "refresh-start",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongodb": "^3.3.4",
    "nodemon": "^1.19.4"
  }
}

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    缺少数据库插入的等待前端:

        try {
          await client.connect();
          const db = client.db();
          const result = await db.collection('products').insertOne(newProduct);
        } catch (error) {
          return res.json({message: 'Could not store data.'});
        };
        client.close();
      
        res.json(newProduct);
      };
      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-06
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2016-07-31
      • 1970-01-01
      相关资源
      最近更新 更多