最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

502未处理的拒绝:ReferenceError:未定义客户端MongoDB

运维笔记admin8浏览0评论

502未处理的拒绝:ReferenceError:未定义客户端MongoDB

502未处理的拒绝:ReferenceError:未定义客户端MongoDB

我正在完成以下教程:-creating-the-api

而且我正在尝试连接我的数据库,但出现上述客户端错误。我已经添加了Mongodb环境。

我有以下文件

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

let cachedDB = null;

// Connect MongoDB
async function connectionDB(uri) {
  if(cachedDB) {
    return cachedDB
  }

  // If there is no DB cache connection. Create one
  const client = await MongoClient.connect(uri, { useNewUrlParser: true });
  // Once connected select the DB
  const db = await client.db(url.parse(uri).pathname.substr(1));

  // Cache and return the DB
  cachedDB = db;
  return db;
}

module.exports = async (req, res) => {
  // Get the db
  const db = await connectionDB(process.env.MONGODB_URI);
  // SElect the DB collection
  const collection = await db.collection('sample_airbnb');
  // Get all records
  const records  = await collection.find({}).toArray();

  // Return the JSON
  res.status(200).json({ records });
}

和以下环境

MONGODB_URI=mongodb+srv://test:[email protected]/admin?retryWrites=true&w=majority

我仅使用示例数据库和数据。

更新1:

> Now CLI 16.3.1 dev (beta) — 
> Ready! Available at http://localhost:3000
> GET /api/airbnb.js
> Building @now/node@latest:api/airbnb.js
Installing dependencies...
> Built @now/node@latest:api/airbnb.js [1s]
(node:18048) DeprecationWarning: 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.
Unhandled rejection: MongoNetworkError: failed to connect to server [test-cluster-shard-00-01-2xnw2.mongodb:27017] on first connect [MongoNetworkError: connection 5 to test-cluster-shard-00-01-2xnw2.mongodb:27017 closed
    at TLSSocket.<anonymous> (/tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/connection.js:352:9)
    at Object.onceWrapper (events.js:299:26)
    at TLSSocket.emit (events.js:209:13)
    at net.js:658:12
    at TCP.done (_tls_wrap.js:479:7) {
  name: 'MongoNetworkError',
  errorLabels: [Array],
  [Symbol(mongoErrorContextSymbol)]: {}
}]
    at Pool.<anonymous> (/tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/topologies/server.js:431:11)
    at Pool.emit (events.js:209:13)
    at /tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/pool.js:580:14
    at callback (/tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/connect.js:109:5)
    at /tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/connect.js:129:7
    at Connection.errorHandler (/tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/connect.js:321:5)
    at Object.onceWrapper (events.js:299:26)
    at Connection.emit (events.js:209:13)
    at TLSSocket.<anonymous> (/tmp/zeit-fun-87b1c66b862dd/node_modules/mongodb/lib/core/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:299:26) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}
LambdaError: RequestId: 6bfc65b3-1f44-4d7e-9c39-f31c17639ba0 Process exited before completing request
    at Lambda.<anonymous> (/usr/lib/node_modules/now/dist/index.js:2:3095633)
    at Generator.next (<anonymous>)
    at fulfilled (/usr/lib/node_modules/now/dist/index.js:2:3093504)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

终端日志

回答如下:

有错别字:

const db = await Client.db(url.parse(uri).pathname.substr(1));

这应该是:

const db = await client.db(url.parse(uri).pathname.substr(1));

注意client的情况。应该是小写字母,因为它是一个变量名。

发布评论

评论列表(0)

  1. 暂无评论