无法将 MongoDB 与 Mongoose 连接
Having Trouble connecting MongoDB with Mongoose
0
我正在学习 Udemy 的 MERN Stack 教程,我正在尝试将 mongodb 与 mongoose 连接起来。我配置的代码在这里:
(db.js)
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');
const connectDB = async () => {
try {
await mongoose.connect(db);
console.log('MongoDB Connect...');
} catch (err) {
console.error(err.message);
//Exit process with falure
process.exit(1);
}
};
module.exports = connectDB;
(server.js)
const express = require('express');
const connectDB = require('./config/db');
const app = express();
//connect database
connectDB();
app.get('/', (req, res) => res.send('API Running'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
(package.json)
{
"name": "devconnect",
"version": "1.0.0",
"description": "Social Network for Developers",
"main": "server.js",
"scripts": {
"start": "node server",
"server": "nodemon server"
},
"author": "Jason Thomas",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.2.5",
"express": "^4.17.1",
"express-validator": "^6.3.1",
"gravatar": "^1.8.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.9",
"request": "^2.88.0"
},
"devDependencies": {
"concurrently": "^5.0.2",
"nodemon": "^2.0.2"
}
}
我进入[npm 运行服务器后]这是我的错误信息
Server started on port 5000
Password contains an illegal unescaped character
[nodemon] app crashed - waiting for file changes before starting...
我想不通,我知道这很简单,我做错了什么,我该如何解决?
最好的问候
& 谢谢
如果您有相同的文件夹结构,您应该有一个像这样的 config/default.json,其中包含您从 mongoDB 集群连接设置中复制粘贴的字符串:
{
"mongoURI": "mongodb+srv://username:<password>@devconnector-clusterid.mongodb.net/somethingSomething?retryWrites=true",
"jwtSecret": "secret",
"githubClientId": "",
"githubSecret": ""
}
请务必在输入密码时删除字符 < >。
新课程的所有文件都在这里 - Devconnector 2.0 In the older version 课程是 config/keys.js .
我最初犯了同样的错误。在 MongoDB URI link 中删除
上的“{}”
0
我正在学习 Udemy 的 MERN Stack 教程,我正在尝试将 mongodb 与 mongoose 连接起来。我配置的代码在这里:
(db.js)
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');
const connectDB = async () => {
try {
await mongoose.connect(db);
console.log('MongoDB Connect...');
} catch (err) {
console.error(err.message);
//Exit process with falure
process.exit(1);
}
};
module.exports = connectDB;
(server.js)
const express = require('express');
const connectDB = require('./config/db');
const app = express();
//connect database
connectDB();
app.get('/', (req, res) => res.send('API Running'));
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
(package.json)
{
"name": "devconnect",
"version": "1.0.0",
"description": "Social Network for Developers",
"main": "server.js",
"scripts": {
"start": "node server",
"server": "nodemon server"
},
"author": "Jason Thomas",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.2.5",
"express": "^4.17.1",
"express-validator": "^6.3.1",
"gravatar": "^1.8.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.8.9",
"request": "^2.88.0"
},
"devDependencies": {
"concurrently": "^5.0.2",
"nodemon": "^2.0.2"
}
}
我进入[npm 运行服务器后]这是我的错误信息
Server started on port 5000
Password contains an illegal unescaped character
[nodemon] app crashed - waiting for file changes before starting...
我想不通,我知道这很简单,我做错了什么,我该如何解决?
最好的问候 & 谢谢
如果您有相同的文件夹结构,您应该有一个像这样的 config/default.json,其中包含您从 mongoDB 集群连接设置中复制粘贴的字符串:
{
"mongoURI": "mongodb+srv://username:<password>@devconnector-clusterid.mongodb.net/somethingSomething?retryWrites=true",
"jwtSecret": "secret",
"githubClientId": "",
"githubSecret": ""
}
请务必在输入密码时删除字符 < >。
新课程的所有文件都在这里 - Devconnector 2.0 In the older version 课程是 config/keys.js .
我最初犯了同样的错误。在 MongoDB URI link 中删除
上的“{}”