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: "[email protected]" };

    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 !!!

12
  • 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. Commented Aug 12, 2018 at 6: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 Commented Aug 12, 2018 at 7:00
  • Put the password and the username in the connection url... Something like this stackoverflow.com/questions/50590080/… Commented Aug 12, 2018 at 8:20
  • @Anthony :I am not using mongoose here,I am using pure mongoclient here,can you suggest me on that basis.? Commented Aug 12, 2018 at 8:25
  • whether it is mongodb or mongoose if you have put username and the password to the database you have to authenticate it... stackoverflow.com/questions/16124255/… Commented Aug 12, 2018 at 8:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.