Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to list collections in playground #415

Open
sunnykeerthi opened this issue Sep 25, 2024 · 1 comment
Open

Unable to list collections in playground #415

sunnykeerthi opened this issue Sep 25, 2024 · 1 comment

Comments

@sunnykeerthi
Copy link

sunnykeerthi commented Sep 25, 2024

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

@lucsoft
Copy link
Collaborator

lucsoft commented Sep 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants