0

I'm writing a chrome extension that needs to access the data from my MongoDB database and store it locally. I've searched around for guides but all of them are Node.js, but I need a solution in native javascript. Security isn't an issue since the data in the database isn't private.

I get an error immediatly when I try to connect to the database (first line).

mongodb.MongoClient.connect('mongodb+srv://<NAME>:<PASSWORD>@<NAME>.a1bc2.mongodb.net/<NAME>?retryWrites=true&w=majority', function(err, db) {
        if (err) throw err;
        var dbo = db.db("PlayerPrices");
        dbo.collection("Playstation").find({}).toArray(function(err, result) {
          if (err) throw err;
          let fullRecord = JSON.parse(result);
          console.log(fullRecord);

          chrome.storage.local.set({ 'playstationRecord': fullRecord }, function() {
            console.log('playstation Data Received');
        });

          db.close();
        });
      });
hampani
  • 129
  • 11
  • 1
    It's **completely insecure** to connect from the user directly to the database! Every user could extract your connection string and send their own queries. I'd recommend you to learn Node.js and write a simple backend. You can even host it for free on Heroku or something. – garzj Sep 13 '20 at 18:10
  • @0nline I have setup so that all users connect with a "read-only" account. I'm using the permissions built in with MongoDB atlas – hampani Sep 13 '20 at 18:12
  • Do you really wanna give all of your users the ability to read information about any other user? – garzj Sep 13 '20 at 18:13
  • @0nline The database doesn't contain any sensitive information about users. All it contains are football player prices at different dates. – hampani Sep 13 '20 at 18:16
  • Oh, I understand. You could try to [browserify](http://browserify.org/) the npm mongodb client. Though I would recommend you to build a simple backend as I think in the end this will be much easier for you. – garzj Sep 13 '20 at 18:28
  • Extensions run in the browser, which is not node.js so it doesn't have any means of connecting to local databases directly. You can write your own middleware utility and invoke it from the extension via [nativeMessaging](https://developer.chrome.com/extensions/nativeMessaging) or enable your database API and connect to it via http port as shown in the linked topic. – wOxxOm Sep 14 '20 at 05:30

0 Answers0