Skip to content

Commit 133b63b

Browse files
committed
fix2
1 parent 3ede472 commit 133b63b

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/backend/db.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@ import DfContentMap from "./types/maps_interface.ts";
55
// Initialize MongoClient
66
const client = new MongoClient();
77
const MONGO_URI = Deno.env.get("MONGO_URI");
8-
if (!MONGO_URI) console.error("MONGO_URI is not set in environment variables (This will crash if DB is accessed)");
8+
9+
console.log("--- DB INIT DEBUG ---");
10+
console.log("CWD:", Deno.cwd());
11+
console.log("MONGO_URI Present:", !!MONGO_URI);
12+
if (MONGO_URI) console.log("MONGO_URI Length:", MONGO_URI.length);
13+
else console.log("⚠️ MONGO_URI IS MISSING from environment!");
14+
console.log("---------------------");
915

1016
let db: any;
1117
let userAuthCollection: any;
1218
let contentMapsCollection: any;
1319

1420
try {
1521
if (MONGO_URI) {
22+
console.log("Attempting to connect to MongoDB...");
1623
await client.connect(MONGO_URI);
1724
db = client.database("df_test");
1825
userAuthCollection = db.collection("user_auth");
1926
contentMapsCollection = db.collection("content_maps");
20-
console.log("Connected to MongoDB successfully");
27+
console.log("✅ Connected to MongoDB successfully");
28+
} else {
29+
console.error("❌ SKIPPING DB CONNECTION: MONGO_URI is missing.");
2130
}
2231
} catch (error) {
23-
console.error("Failed to connect to MongoDB", error);
32+
console.error("Failed to connect to MongoDB:", error);
2433
}
2534

2635
// Function to update access token on db if user exists

src/backend/dependencies.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import { create, verify } from "https://deno.land/x/djwt@v2.9.1/mod.ts";
1010
import { exec } from "https://deno.land/x/exec@0.0.5/mod.ts";
1111
import * as Sentry from 'https://deno.land/x/sentry/index.mjs';
1212
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
13-
import "https://deno.land/std@0.224.0/dotenv/load.ts";
14-
import { MongoClient, ObjectId } from "https://deno.land/x/mongo@v0.32.0/mod.ts";
13+
import { config } from "https://deno.land/std@0.224.0/dotenv/mod.ts";
14+
import { MongoClient, ObjectId } from "https://deno.land/x/mongo@v0.33.0/mod.ts";
15+
16+
try {
17+
await config({ export: true }); // Try default .env in CWD
18+
// If running from root, try loading src/backend/.env
19+
await config({ export: true, path: "./src/backend/.env" });
20+
} catch (e) {
21+
// Ignore errors if file already loaded or not found
22+
}
1523

1624
export {
1725
Application,

src/backend/test_db_deno.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { MongoClient } from "https://deno.land/x/mongo@v0.32.0/mod.ts";
2+
import "https://deno.land/std@0.224.0/dotenv/load.ts";
3+
4+
const MONGO_URI = Deno.env.get("MONGO_URI");
5+
6+
console.log("----------------------------------------");
7+
console.log("DEBUGGING MONGODB CONNECTION");
8+
console.log("----------------------------------------");
9+
10+
if (!MONGO_URI) {
11+
console.error("❌ ERROR: MONGO_URI is NOT set in the environment.");
12+
console.log("Please check your .env file.");
13+
} else {
14+
// Mask password for logging
15+
const maskedUri = MONGO_URI.replace(/:([^@]+)@/, ":****@");
16+
console.log(`✅ MONGO_URI found: ${maskedUri}`);
17+
}
18+
19+
const client = new MongoClient();
20+
21+
async function testConnection() {
22+
if (!MONGO_URI) return;
23+
24+
try {
25+
console.log("Attempting to connect with Deno driver...");
26+
await client.connect(MONGO_URI);
27+
console.log("✅ SUCCESS: Connected to MongoDB!");
28+
29+
const db = client.database("df_test");
30+
const collections = await db.listCollectionNames();
31+
console.log("✅ Collections found:", collections);
32+
33+
client.close();
34+
} catch (error) {
35+
console.error("❌ FAILURE: Could not connect to MongoDB.");
36+
console.error("Error details:", error);
37+
38+
// Check for common SRV issues
39+
if (MONGO_URI.includes("mongodb+srv://")) {
40+
console.log("\n⚠️ NOTE: You are using a 'mongodb+srv://' connection string.");
41+
console.log("If this fails in Deno but works in Node, it might be a DNS resolution issue with the Deno driver.");
42+
console.log("Try using the standard 'mongodb://' connection string if possible.");
43+
}
44+
}
45+
}
46+
47+
testConnection();

0 commit comments

Comments
 (0)