Question

This is a part of a corporate project. I am trying to host an application on the stackato server put up by my company. My app has a node framework with mongodb support. I wrote a simple script that tries to connect to the db service and insert a JSON object. I am able to successfully connect to the database but on inserting, returns an error saying:

MongoError: not authorized for insert on db:componentlist

My code:

var mongo = require('mongodb');
var Db = mongo.Db;
var Server = mongo.Server;
var services;
var dbcreds;

var dbcreds = {
  //cant paste due to confidential reasons
};

  var server = new Server(
          dbcreds.host,
          dbcreds.port, 
          {user: dbcreds.username, pass: dbcreds.password}
      );

  db = new Db(dbcreds.db, server,{w:1});
  console.log('connecting to online db and now storing data in:' + dbcreds.db);


db.open(function(err, db) {
    if (err) { console.log('db connection error: ' + err);}
    console.log('db is opened.');

    var component = [
    {
        platform: "alpha",
        component1: "beta",

    }];

    db.collection('componentlist', function(err,collection){
        collection.insert(component, function(err, result){
        if(err){console.log("cannot insert the object into componentlist " + err);}         
        db.close();
        });
    });     

});

The terminal returns this when I run this on my machine which has node and mongodb installed. These are a set of console log messages that you can relate to the code

connecting to online db and now storing data in:db, db is opened., cannot insert the object into componentlist MongoError: not authorized for insert on db.componentlist

I checked the db online to reconfirm that no data has been inserted successfully. Am I missing any extra authorization commands required?

Was it helpful?

Solution

Not sure of the Server options for authentication. The preferred method now is to use the MongoClient class for connections like so:

var MongoClient = require("mongodb").MongoClient;

MongoClient.connect(
    "mongodb://<user>:<pass>@<host>:<port>/<db>", function(err, db) {

    // everything else inside your "open.db" call

Which is preferred, but just in case there is something wrong with the way credentials are being passed in, as this way is well documented. This is just to make sure you are connecting as the user account you are intending to use.

Beyond that, as long as this is the right user, then the error suggests that this user lacks the permission to do the insert on the database. So you might need to check the permissions that have been granted to the use. So the account would need the readWrite role to be applied.

OTHER TIPS

Sorry if this is a very late response but maybe this can help others with a similar problem. But I ran into the same issue in my Stackato environment. So it seems that you have your database name set to "componentlist" but in Stackato it will do a mongodb URI substitution which often has a random generated username, password and certain fixed set host and port and if you log into your stackato instance using stackato ssh and run the "env" command you may see the exact URI used to connect to the mongodb service created by stackato. The database name is most likely "db" and since you are trying to connect to "componentlist" you will get an authorization failure.

If you change your database name to "db" it might work. I am trying to figure out how to set the database name to something more specific on my stackato.yml file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top