首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >replit.com -用秘密连接到猫鼬

replit.com -用秘密连接到猫鼬
EN

Stack Overflow用户
提问于 2021-04-17 18:47:16
回答 1查看 575关注 0票数 0

我对express.js和猫鼬很陌生,并试图从freeCodeCamp中解决一些例子。我有一种奇怪的行为,找不到,这是什么问题。

以这种方式连接到数据库的工作方式:

代码语言:javascript
复制
require('dotenv').config();
var mongoose = require('mongoose');

mongoose.connect('mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true });

代码不起作用:

与秘密联系是行不通的。秘密称为: MONGO_URI,值完全相同(console.log(mySecret) ->完全相同的字符串,也包括‘。

代码语言:javascript
复制
require('dotenv').config();
var mongoose = require('mongoose');
var mySecret = process.env['MONGO_URI']

mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });

我遗漏了什么?

package.json ->依赖项:

代码语言:javascript
复制
"dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongodb": "^3.6.6",
    "mongoose": "^5.12.0"
},

下面是控制台内的消息(使用秘密):

代码语言:javascript
复制
Your app is listening on port 3000
(node:1022) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/uri_parser.js:50:23)
    at parseConnectionString (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/core/uri_parser.js:595:12)
    at connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/operations/connect.js:281:3)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/mongo_client.js:256:5
    at maybePromise (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/utils.js:685:3)
    at MongoClient.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongodb/lib/mongo_client.js:252:10)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:834:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/connection.js:831:19)
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:348:10
    at /home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:1152:10)
    at Mongoose.connect (/home/runner/boilerplate-mongomongoose-1/node_modules/mongoose/lib/index.js:347:20)
    at Object.<anonymous> (/home/runner/boilerplate-mongomongoose-1/myApp.js:6:10)
(node:1022) 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: 3)
(node:1022) [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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-20 08:00:55

在项目的根目录中创建一个.env文件。以NAME=VALUE形式在新行中添加特定于环境的变量。

MONGO_URI文件中定义.env,如下所示

代码语言:javascript
复制
NODE_ENV=development
MONGO_URI=mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

在你需要的地方访问它

代码语言:javascript
复制
const mySecret = process.env['MONGO_URI'];

.ENV使用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67141819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档