'Connecting to Deno MongoDb in Docker throws uncaught promise (in wsl2)
The project runs in Deno.
I'm trying to connect the MongoClient in Deno to a MongoDb container running in Docker.
The docker container is running in wsl2 with the wsl ip 172.18.96.38 and exposing port 27017.
Connecting to it with the following code leads to an error:
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
const uri = 'mongodb://admin:[email protected]:27017'
this.mClient = new MongoClient();
await this.mClient.connect(uri);
error: Uncaught (in promise) Error: MongoError: "Connection failed: MongoError: {\"ok\":0,\"errmsg\":\"no such command: 'hello'\",\"code\":59,\"codeName\":\"CommandNotFound\"}"
throw new MongoError(`Connection failed: ${e.message || e}`);
^
at MongoClient.connect (https://deno.land/x/[email protected]/src/client.ts:26:13)
at async Function.initMongoClient (file:///home/robert/repos/myDenoWebsite/backend/src/utils/mongoClientWrapper.ts:54:9)
According to the errmsg the error is 'no such command: 'hello'. What does this mean?
This is my docker-compose.yml for mongo btw:
version: '3.8'
services:
mongodb:
image: mongo:4.4-bionic
container_name: my-mongodb
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
volumes:
- mongodb:/data/db
- mongoconfig:/data/configdb
volumes:
mongodb:
mongoconfig:
Solution 1:[1]
I got a same error, so I found an alternative way
This is my solution
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
const conn = async () => {
const client = new MongoClient();
const srv = `mongodb+srv://admin:[email protected]:27017/<Your Database>?authMechanism=SCRAM-SHA-1&retryWrites=true&w=majority`
await client.connect(srv);
return client;
}
export const getData = async () => {
const conn = await conn()
<Your Logic Here>
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
