replit.com - 使用 Secrets 连接到猫鼬
replit.com - connecting to mongoose with Secrets
我是 express.js 和 mongoose 的新手,正在尝试解决 freeCodeCamp 中的一些示例。
我有一个奇怪的行为,找不到,这里有什么问题。
以这种方式连接到数据库有效:
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) --> 完全相同的字符串,也包括 ' .
require('dotenv').config();
var mongoose = require('mongoose');
var mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
我错过了什么?
package.json -> 依赖关系:
"dependencies": {
"body-parser": "^1.15.2",
"dotenv": "^8.2.0",
"express": "^4.12.4",
"mongodb": "^3.6.6",
"mongoose": "^5.12.0"
},
这是控制台中的消息(使用 Secrets):
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.
在项目的根目录中创建一个 .env 文件。以 NAME=VALUE
的形式在新行上添加特定于环境的变量
在 .env
文件中像这样定义你的 MONGO_URI
NODE_ENV=development
MONGO_URI=mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
并在需要的地方访问它
const mySecret = process.env['MONGO_URI'];
我是 express.js 和 mongoose 的新手,正在尝试解决 freeCodeCamp 中的一些示例。 我有一个奇怪的行为,找不到,这里有什么问题。
以这种方式连接到数据库有效:
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) --> 完全相同的字符串,也包括 ' .
require('dotenv').config();
var mongoose = require('mongoose');
var mySecret = process.env['MONGO_URI']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
我错过了什么?
package.json -> 依赖关系:
"dependencies": {
"body-parser": "^1.15.2",
"dotenv": "^8.2.0",
"express": "^4.12.4",
"mongodb": "^3.6.6",
"mongoose": "^5.12.0"
},
这是控制台中的消息(使用 Secrets):
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.
在项目的根目录中创建一个 .env 文件。以 NAME=VALUE
的形式在新行上添加特定于环境的变量在 .env
文件中像这样定义你的 MONGO_URI
NODE_ENV=development
MONGO_URI=mongodb+srv://<user>:<password>@cluster0.omzte.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
并在需要的地方访问它
const mySecret = process.env['MONGO_URI'];