|
| 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