I'm trying to list collections using the below code in playground. I see only the message Connected!!! in output.
import { createServer } from "node:http";
import process from "node:process";
import { MongoClient, Bson } from "https://deno.land/x/mongo@v0.33.0/mod.ts";
const url =
"mongodb+srv://userName:password@book-an-appointment-clu.rajwa.mongodb.net/?authMechanism=SCRAM-SHA-1";
const client = new MongoClient();
const dbName = "appointments-db";
let x=[];
const server = createServer(async (req, res) => {
try {
const db = await client.connect(url);
const names = await db.listCollections().toArray();
names.forEach((collection) =>
x.push(collection.name)
);
res.end("Connected!!!"+ await x+" ");
} catch (error) {
res.end("An error occurred:", error);
throw error;
} finally {
await client.close();
console.log("Connection closed.");
}
});
server.listen(8080);
also when I add const database=await client.db("dbName"), I get an error as TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: TypeError: client.db is not a function
Its the same when I use url as const uri = "mongodb+srv://userName:password@book-an-appointment-clu.rajwa.mongodb.net/?retryWrites=true&w=majority&appName=book-an-appointment-cluster";
Please lemme know where I'm going wrong.
Please take a look at the readme. The API is different then how you are using it, connect doesn't return anything. And i would suggest you to use Deno.serve(() => new Response("Hello World")) instead of node:http
I'm trying to list collections using the below code in playground. I see only the message
Connected!!!
in output.also when I add
const database=await client.db("dbName")
, I get an error asTypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: TypeError: client.db is not a function
Its the same when I useurl
asconst uri = "mongodb+srv://userName:password@book-an-appointment-clu.rajwa.mongodb.net/?retryWrites=true&w=majority&appName=book-an-appointment-cluster";
Please lemme know where I'm going wrong.Thanks, Sunny