Skip to content

Unable to list collections in playgroundΒ #415

@sunnykeerthi

Description

@sunnykeerthi

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/[email protected]/mod.ts";
const url =
  "mongodb+srv://userName:[email protected]/?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:[email protected]/?retryWrites=true&w=majority&appName=book-an-appointment-cluster";
Please lemme know where I'm going wrong.

Thanks,
Sunny

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions