1

I have created one sample application for connection of mongodb and nodejs. I got error like MongoError: there are no users authenticated

after that I have added username and password to the mongodb connection string like "mongodb://root:root@localhost:27017/marankings"

so authentication eror has gone and it gives me new error like

Buffer.from('n=', 'utf8'), ^

TypeError: utf8 is not a function at Function.from (native) at Function.from (native)

Please guide me on this , I am stuck here.

This is my code

connectToServer: function(callback) {
  MongoClient.connect("mongodb://root:root@localhost:27017/marankings", function(err, db) {
    //db = db;
    var dbcon=db.db("marankings");
    console.log("this is connected db"+_db);
    var myobj = { name: "Akshay Champavat", address: "akshay@gmail.com" };

    dbcon.collection("users").insertOne(myobj, function(err, res) {
      if (err) throw err;
      console.log("1 document inserted");
      dbcon.close();
    });

    return callback(err);
  });
},

Please help , Thanks in advance !!!

  • You may be calling `callback` too early. Have you try puting it in the `insertOne` callback? What does the db look like, is your user really inserted? Also please show us how you retrieve your user. – Iso Aug 12 '18 at 06:41
  • @Iso :hey thanks for reaching out me User is not inserted ,only I got db connection successfully . after that it wont allow me to insert user in users collection.@Iso – Akshay Champavat Aug 12 '18 at 07:00
  • Put the password and the username in the connection url... Something like this https://stackoverflow.com/questions/50590080/unhandled-promise-rejection-error-url-malformed-cannot-be-parsed/50590439#50590439 – Ashh Aug 12 '18 at 08:20
  • @Anthony :I am not using mongoose here,I am using pure mongoclient here,can you suggest me on that basis.? – Akshay Champavat Aug 12 '18 at 08:25
  • whether it is mongodb or mongoose if you have put username and the password to the database you have to authenticate it... https://stackoverflow.com/questions/16124255/how-to-connect-with-username-password-to-mongodb-using-native-node-js-driver – Ashh Aug 12 '18 at 08:25
  • @Anthony : ok , I did it but it gives error like : express-mongo/node_modules/mongodb-core/lib/auth/scram.js:189 Buffer.from('n=', 'utf8'), ^ TypeError: utf8 is not a function at Function.from (native) at Function.from (native) at executeScram (/var/www/html/nodejs/express-mongo/node_modules/mongodb-core/lib/auth/scram.js:189:14) at /var/www/html/nodejs/express-mongo/node_modules/mongodb-core/lib/auth/scram.js:361:7 at nextTickCallbackWith0Args (node.js:419:9) at process._tickCallback (node.js:348:13) – Akshay Champavat Aug 12 '18 at 08:35
  • please edit your question instead of commenting – Ashh Aug 12 '18 at 08:39
  • @AnthonyWinzlet:Please check now . – Akshay Champavat Aug 12 '18 at 08:53
  • what is your node version and mongodb version? – Ashh Aug 12 '18 at 09:17
  • @Anthony : node 4.2.6 and mongodb mongodb 3.6.4 db version v3.6.4 git version: d0181a711f7e7f39e60b5aeb1dc7097bf6ae5856 OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016 allocator: tcmalloc modules: none build environment: distmod: ubuntu1604 distarch: x86_64 target_arch: x86_64 – Akshay Champavat Aug 12 '18 at 09:27
  • Well update your node version if you can otherwise you can try this https://stackoverflow.com/questions/42260671/typeerror-base64-is-not-a-function-when-using-buffer-from-in-node-4-3 – Ashh Aug 12 '18 at 09:37
  • @AnthonyWinzlet : thanks champ ! its resolved , upgrading node version works for me . – Akshay Champavat Aug 12 '18 at 11:37

0 Answers0