gpt4 book ai didi

node.js - 连接 MongoDB 与 Mongoose 时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:38 24 4
gpt4 key购买 nike

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;

(服务器.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}`));

(包.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 run server]这是我的错误消息

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": ""
}

输入密码时,请务必删除字符 < >。

新类(class)的所有文件都在这里 - Devconnector 2.0older version当然是 config/keys.js 。

关于node.js - 连接 MongoDB 与 Mongoose 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59805813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com