-1

I'm doing a small hobby project for learning how to connect and read information from a database. Have done it with a local host JSON server, but want to take it a step further. I'm trying to connect to a MongoDB server inside of a Vue 3 component. The end goal is to read user login credentials from a database and compare it to input from a form. But first, I'm trying to get some simple information to work with to learn the basic concepts. This code list the databases I have in my cluster and works fine when I run it through an index.js file with the node terminal (Except the fake connection string).

  const {
        MongoClient
    } = require('mongodb');
    
    async function main() {
        const uriTest = "mongodb+srv://notRealUser:password@cluster0.p3imp.mongodb.net/database?authSource=admin&retryWrites=true&w=majority"
        const client = new MongoClient(uriTest);
        try {
            await client.connect();
            await listDatabases(client);
            console.log("Connected correctly to server");
        } catch (e) {
            console.error(e);
        } finally {
            await client.close();
        }
    }
    
    main().catch(console.error);
    
    async function listDatabases(clients) {
        const databasesList = await clients.db().admin().listDatabases();
        databasesList.databases.forEach(db => console.log(`${db.name}`));
    }

But whenever I want to run it in my Vue 3 application I get a long error. I get about 27 errors mentioning something similar to this:

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }

I searched the internet for an answer and someone mentioned that this could be because of "web pack 5". I would really like to learn how to solve this for a Vue project. Is it a better way to approach this with MongoDB and Vue3? If I'm doing this all wrong please lecture me. Thank you for reading. :)

  • 1
    Vue is client-side JS that runs in the browser. The mongodb library you're requiring is for server-side JS, i.e. nodeJS. To use mongodb, you create a backend API server (typically using [express](https://expressjs.com/)) where you connect to your mongodb instance. Then you request your own API routes from the server, basically forwarding DB data to your client-side Vue app. –  Apr 19 '22 at 22:00
  • Ok thank you! So basically learn express and use that to talk with the MongoDB database? – Kim Oliver Andersson Apr 19 '22 at 22:05
  • 1
    Yes, I recommend starting with a basic express website in order to learn how GET and POST requests work and how the client and server-side interact. –  Apr 19 '22 at 22:07
  • I will totally do this. learn the basics of express and then try to implement this to Vu3 when I'm comfortable. Thank you for helping me a step further on my journey! Have a good day/night :) – Kim Oliver Andersson Apr 19 '22 at 22:13
  • 1
    what's mAngodb o.O – mstephen19 Apr 19 '22 at 22:21
  • Try [this](https://stackoverflow.com/questions/68206050/breaking-change-webpack-5-used-to-include-polyfills-for-node-js-core-modules) for webpack error. i had a same error, worked for me. – nio Aug 25 '22 at 05:15

1 Answers1

1

It's rather not possible to connect to the database directly from the website and even if it's possible, no one will recommend this.

To access any database you should have a code that is running on the server side where malicious users can't access it. This is why web application usually need a backend.

Shivam
  • 3,514
  • 2
  • 13
  • 27
Konrad
  • 21,590
  • 4
  • 28
  • 64