MongoDB connection
Hello, I'm very new to coding so please bear with me π
I followed a tutorial to build a blog using a MEJN stack, and it would connect to Mongo ok locally but when I'm deploying on railway it won't seem to connect.
Looking at the docs, I can't seem to figure out what to do. Can you help? What info can I provide you with?
4 Replies
Project ID:
N/A
what error are you getting?
Error: Cannot init client. Please provide correct options
at new MongoStore (/app/node_modules/connect-mongo/build/main/lib/MongoStore.js:123:19)
at MongoStore.create (/app/node_modules/connect-mongo/build/main/lib/MongoStore.js:140:16)
at Object.<anonymous> (/app/app.js:21:33)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
Node.js v18.18.2
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const methodOverride = require("method-override");
const expressLayout = require("express-ejs-layouts");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
// const dbString = process.env.MONGO_DB_URI;
const connection = mongoose.createConnection(process.env.MONGO_DB_URI);
const helmet = require("helmet");
const app = express();
const PORT = 3000 || process.env.PORT;
const connectDB = require("./server/config/db.js");
const { isActiveRoute } = require("./server/helpers/routeHelpers.js");
const sessionStore = MongoStore.create({
client: connection.getClient(),
collection: "session",
});
// Connect to Databse
connectDB();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.use(cors());
app.use(methodOverride("_method"));
app.use(
session({
secret: "",
resave: false,
saveUninitialized: true,
store: sessionStore,
})
);
app.use(express.static("public"));
// Templating Engine
app.use(expressLayout);
app.set("layout", "./layouts/main");
app.set("view engine", "ejs");
app.locals.isActiveRoute = isActiveRoute;
app.use("/", require("./server/routes/main.js"));
app.use("/", require("./server/routes/admin.js"));
app.listen(PORT, () => {
console.log(
App listening on port ${PORT}
);
});
// Security
app.use(
helmet.contentSecurityPolicy({
directives: {
"script-src": ["'self'", "code.jquery.com", "cdn.jsdelivr.net"],
},
})
);
It seems be something to do with MongoStore, but it's not something I'm familiar with.
I try changing it based on a few comments on stackoverflow and medium, but can't resolve it
Actually I seem to have fixed that issuen now, but he app still crashes. It runs locally no problem, and the logs don't show any issues π€do you have a
MONGO_DB_URI
service variable set?