I'm trying to connect to a MongoDB Atlas database with typeORM in an express project but I'm getting a 'Unescaped slash in userinfo section' error. My password doesn't have any special characters that need to be encoded so I don't know what's going on.
import { DataSource } from "typeorm"
export const AppDataSource = new DataSource({
type: "mongodb",
host: "mongodb+srv://username:password@database.cluster.mongodb.net/?retryWrites=true&w=majority",
useNewUrlParser: true,
synchronize: true,
useUnifiedTopology: true,
logging: true,
ssl: true,
entities: [
"src/entity/*.ts"
],
subscribers: [],
migrations: [],
})
"use strict";
import express from "express";
import cors from 'cors';
import "reflect-metadata";
import { AppDataSource } from "./data-source";
AppDataSource.initialize().then(() => {
const app = express();
app.use(cors({
origin: ['http://localhost:3000'],
credentials: true // this will allow cookies to be sent accross domains
}));
app.listen(8080, () => {
console.log("Server is running on port 8080");
})
})
the error I'm getting:
return callback(new MongoParseError('Unescaped slash in userinfo section'));
^
MongoParseError: Unescaped slash in userinfo section
at parseConnectionString (D:\Personal\jamboit\backend\node_modules\mongodb\lib\core\uri_parser.js:627:21)
at connect (D:\Personal\jamboit\backend\node_modules\mongodb\lib\operations\connect.js:283:3)
at D:\Personal\jamboit\backend\node_modules\mongodb\lib\mongo_client.js:284:5
at maybePromise (D:\Personal\jamboit\backend\node_modules\mongodb\lib\utils.js:692:3)
at MongoClient.connect (D:\Personal\jamboit\backend\node_modules\mongodb\lib\mongo_client.js:280:10)
at Function.MongoClient.connect (D:\Personal\jamboit\backend\node_modules\mongodb\lib\mongo_client.js:426:22)
at D:\Personal\jamboit\backend\node_modules\typeorm\src\driver\mongodb\MongoDriver.ts:249:38
at new Promise (<anonymous>)
Just replace host params to url in DataSource options
export const AppDataSource = new DataSource({
type: "mongodb",
url: "mongodb+srv://username:password@database.cluster.mongodb.net/?retryWrites=true&w=majority",
useNewUrlParser: true,
synchronize: true,
useUnifiedTopology: true,
logging: true,
ssl: true,
entities: [
"src/entity/*.ts"
],
subscribers: [],
migrations: [],
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.