0

I'm getting an error when submitting a form (bad request: 400 error).

{name: "MongoError", connectionId: 75476,

err: "not authorized for insert on database-name.shipper",…}

code: 13

connectionId:75476

err:"not authorized for insert on database-name.shipper"

n:0

name:"MongoError"

ok:1

It works fine when set up to record data in a local Mongo database, and I can see the data, but when I'm using mlab I get this error in the console when I try to submit the form to record the info in the database. When I start my app using nodemon app.js, I get: Express server listening on port 80 mongo :: connected to database :: "database-name"

This is my code I have in account-manager.js:

var crypto = require('crypto');

var moment = require('moment');

var stringify = require('json-stringify-safe');

var MongoDB = require('mongodb').Db;

var Server = require('mongodb').Server;

var mongoose = require('mongoose');


var dbName = process.env.DB_NAME || 'database-name';

var dbHost = process.env.DB_HOST || 'ds053176.mlab.com';

var dbPort = process.env.DB_PORT || 53176;

var dbUser = process.env.DB_USER || 'user@emailaddress.com';

var dbPass = process.env.DB_PASS || 'passwordtoaccount';


var db = new MongoDB(dbName, new Server(dbHost, dbPort, {auto_reconnect: true}), {w: 1});

db.open(function(e, d){

    if(e) {

        console.log(e);

    } else {

        if (process.env.NODE_ENV == 'live') {

            db.authenticate(dbUser, dbPass, function(e, res) {

                if (e) {

                    console.log('mongo :: error: not authenticated', e);

                } else {

                    console.log('mongo :: authenticated and connected :: "'+dbName+'"');

                }

            });

        } else {

            console.log('mongo :: connected to database :: "'+dbName+'"');

        }

    }
});


var accounts = db.collection('accounts');

var shipper = db.collection('shipper');

var trucker = db.collection('trucker');

I've been looking for a solution but haven't found one. Any help or anyone who can lead me in the right direction would be greatly appreciated!

Community
  • 1
  • 1
Allison Schambers
  • 321
  • 1
  • 5
  • 12
  • I would recommend using MongoClient `require('mongodb').MongoClient.connect('mongodb://:@:/db-name', function(err, db){ ... });` see example here https://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#authenticate – Molda Oct 11 '16 at 06:24
  • Does the mongodb user in mlab has write permissions?? – mkorszun Oct 11 '16 at 08:28
  • I was able to solve the problem by reading this thread on stackoverflow: http://stackoverflow.com/questions/13980236/does-mongodb-have-reconnect-issues-or-am-i-doing-it-wrong/14409431#14409431. I was really looking for how to set up the database connection to mlab, and one of the answers was perfect. Thanks for the suggestions though everyone. And yes, the database had read and write permissions, the problem was the way I set up. – Allison Schambers Oct 11 '16 at 09:13

0 Answers0