Open Massayu opened 7 months ago
On my tests above i had the npm imports on deno.json now i tried with npm:
import { MongoClient } from "npm:mongodb@6.1.0";
const client = new MongoClient("mongodb+srv://...");
await client.connect();
console.log("Connected successfully to server");
C:\Users\Documents\NodejsConsoleApp1>deno run --allow-all app.js
error: Uncaught (in promise) MongoServerSelectionError: connection timed out
at Timeout._onTimeout (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/sdam/topology.js:278:38)
at cb (ext:deno_node/internal/timers.mjs:63:31)
at Object.action (ext:deno_web/02_timers.js:154:11)
at handleTimerMacrotask (ext:deno_web/02_timers.js:68:10)
at eventLoopTick (ext:core/01_core.js:173:21)
Caused by: MongoNetworkTimeoutError: connection timed out
at connectionFailureError (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/cmap/connect.js:381:20)
at TLSSocket.<anonymous> (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/cmap/connect.js:285:22)
at Object.onceWrapper (ext:deno_node/_stream.mjs:1929:32)
at TLSSocket.emit (ext:deno_node/_stream.mjs:1854:9)
at TLSSocket._onTimeout (node:net:848:10)
at cb (ext:deno_node/internal/timers.mjs:63:31)
at Object.action (ext:deno_web/02_timers.js:154:11)
at handleTimerMacrotask (ext:deno_web/02_timers.js:68:10)
at eventLoopTick (ext:core/01_core.js:173:21)
@littledivy
I've come across this issue and think the root cause is only when it's run on Windows. I didn't see this issue, wrote my own, then did 1 more search before posting and found this. So I'll just paste my issue here.
@Massayu - It might be worth updating the title to something like this for clarity, assuming you agree with my outcome
Unable to connect to mongodb Atlas server using mongoose & mongodb+srv:// record on windows
When trying to connect to MongoDB Atlas using mongoose driver and a mongodb+srv:// record, the connection returns MongooseServerSelectionError: connection timed out
The issue seems to only occur
A related issue raised on the mongoose GitHub (linked further below) raises a question that it seems to be only for some WiFi network configs. I've tested this on a dektop PC hardwired & on a laptop with WiFI. Both have the same issue.
I have Windows Sandbox installed on both of my machines. That's the only think I can think of that could be messing with a network connection (massive stretch though). So can someone without it check it's still an issue.
URI with mongodb://
URI with mongodb+srv://
Passing it through resolve_mango_dns
Hardcoding the user/pass/server into the connection string so no variable issues
using the --unstable flag & the --allow-all
ensure all packages are up-to-date
ensure that my IP address is white-listed in mongo db
Installed Mongosh CLI onto my PC
Setup a mongo db image on Docker Desktop using image mongodb/mongodb-community-server:latest
deno 1.44.4 (release, x86_64-pc-windows-msvc) ...Ubuntu Version: deno 1.44.4 (release, x86_64-unknown-linux-gnu) v8 12.6.228.9 typescript 5.4.5 "mongoose": "npm:mongoose@8.4.4" Windows 11 v23H2 (OS Build 22631.3737)
This seems to be related to an issue on the mongoose GitHub page, but was closed as 'underlying library issue' with a recommendation to upgrade to mongoose v8, however that doesn't seem to have helped. https://github.com/Automattic/mongoose/issues/13988
Original issue seems to only happen on Windows, only on Deno, only with Atlas, only on certain WiFi, and only on Mongoose 7. I'm able to repro with Mongoose 7.6.3 on @IslandRhythms 's machine, but the issue comes down to the fact that tls.connect() hangs here. The socket never emits any events. There isn't much more we can do to fix this particular error, other than recommend upgrading to Mongoose 8, which seems to be unaffected.
connectionString mongodb+srv://<REMOVED_USER>:<REMOVED_PASS>@cluster0.<REMOVED>.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
error: Uncaught (in promise) MongooseServerSelectionError: connection timed out
at _handleConnectionErrors (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongoose/8.4.3/lib/connection.js:897:11)
at NativeConnection.openUri (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongoose/8.4.3/lib/connection.js:848:11)
at eventLoopTick (ext:core/01_core.js:207:9)
at async DB.connect (file:///D:/DevWork/<REMOVED_MY_APP_NAME>/config/db.ts:62:5)
at async file:///D:/DevWork/<REMOVED_MY_APP_NAME>/config/db.ts:79:1
Watcher Process failed. Restarting on file change...
(To be honest, I've never done any debugging like this and thought if I tried I might be able to find something. I was wrong - I have no idea, so ignore if this doesn't help)
MongoNetworkTimeoutError: connection timed out
at connectionFailureError (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongodb/6.6.2/lib/cmap/connect.js:355:20)
at TLSSocket.<anonymous> (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongodb/6.6.2/lib/cmap/connect.js:269:43)
at Object.onceWrapper (ext:deno_node/_stream.mjs:1926:32)
at TLSSocket.emit (ext:deno_node/_stream.mjs:1851:9)
at TLSSocket._onTimeout (node:net:849:10)
at cb (ext:deno_node/internal/timers.mjs:64:31)
at eventLoopTick (ext:core/01_core.js:203:13)
deno-x86_64-pc-windows-msvc v1.41.1
Testing the following code using
nodejs
and ondeno deploy
works correctly:But when testing using
deno cli
it doesn't work.I get the message on the console
starting...
but not theconnection error:
neitherConnected to MongoDB
And after some time I get this error:
The same thing happens using
mongodb
instead ofmongoose
:I'm starting the script as
I also tested running it as
admin
, on the Mongo website I have all ip whitelisted Why do I get this error only on the CLI version?