【问题标题】:(node:18896) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated error(node:18896) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated error
【发布时间】:2020-07-08 14:13:36
【问题描述】:

我正在使用 mongoDB atlas cluster 和 express.js。我遇到了这个错误,我尝试了在网上找到的不同解决方案,但没有一个有效。

当我尝试通过邮递员在 createProduct 方法上发送发布请求时出现此错误

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

const url = "mongodb+srv://xxx:xxx@cluster0-05vvl.mongodb.net/test?retryWrites=true&w=majority";

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) {
    console.log(error);
    return res.json({message: 'Could not store data.'});
  };
  client.close();

  res.json(newProduct);
};

const getProducts = async (req, res, next) => {};

exports.createProduct = createProduct;
exports.getProducts = getProducts;


【问题讨论】:

标签: node.js mongodb


【解决方案1】:

问题是,以前当我添加 cmongoDB atlas 集群时,我添加了 ip 以访问该服务器。当您使用其他 ip 时,它将无法工作。为 mongoDB ATLAS 添加选项以获取两个 id 或选择“访问” ALL' 它会起作用。这就是我的错误的原因。

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2022-06-30
    • 2022-01-15
    • 2022-08-20
    • 1970-01-01
    • 2022-12-27
    • 2018-07-30
    • 2016-02-28
    相关资源
    最近更新 更多