简体   繁体   中英

Getting error while inserting data into MongoDB using Node.js and mongoLAB

I am getting following error while inserting data into MongoDB present inside MongoLab using Node.js.

Error:

err { MongoError: not authorized on fgdp to execute command { insert: "f_user_login", documents: [ { email: "a@gmail.com", password: "0d7f943d633e49fb06fe6c53acabe3c5", dob: "01-08-2017", created_date: "2017-08-01 19:05:28", updated_date: "2017-08-01 19:05:28", status: 0, token: "", _id: ObjectId('598083a27d8f73686c43a55a') } ], ordered: true, writeConcern: { w: 1 } }
    at Function.MongoError.create (/opt/lampp/htdocs/heroku/FGDP/node_modules/mongodb-core/lib/error.js:31:11)
    at /opt/lampp/htdocs/heroku/FGDP/node_modules/mongodb-core/lib/connection/pool.js:483:72
    at authenticateStragglers (/opt/lampp/htdocs/heroku/FGDP/node_modules/mongodb-core/lib/connection/pool.js:429:16)
    at Connection.messageHandler (/opt/lampp/htdocs/heroku/FGDP/node_modules/mongodb-core/lib/connection/pool.js:463:5)
    at Socket.<anonymous> (/opt/lampp/htdocs/heroku/FGDP/node_modules/mongodb-core/lib/connection/connection.js:319:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)
  name: 'MongoError',
  message: 'not authorized on fgdp to execute command { insert: "f_user_login", documents: [ { email: "a@gmail.com", password: "0d7f943d633e49fb06fe6c53acabe3c5", dob: "01-08-2017", created_date: "2017-08-01 19:05:28", updated_date: "2017-08-01 19:05:28", status: 0, token: "", _id: ObjectId(\'598083a27d8f73686c43a55a\') } ], ordered: true, writeConcern: { w: 1 } }',
  ok: 0,
  errmsg: 'not authorized on fgdp to execute command { insert: "f_user_login", documents: [ { email: "a@gmail.com", password: "0d7f943d633e49fb06fe6c53acabe3c5", dob: "01-08-2017", created_date: "2017-08-01 19:05:28", updated_date: "2017-08-01 19:05:28", status: 0, token: "", _id: ObjectId(\'598083a27d8f73686c43a55a\') } ], ordered: true, writeConcern: { w: 1 } }',
  code: 13 }

My code is below:

var mongoJs=require('mongojs');
var mden = require('md5');
var dateTime = require('node-datetime');
var collections=['f_users','f_user_login'];
var MONGOLAB_URI="mongodb://user:*******%40@ds127153.mlab.com:27153/fgdp";
var db=mongoJs(MONGOLAB_URI,collections);
exports.userSignup=function(req,res){
    var email=req.body.email;
    var password=req.body.password;
    var dob=req.body.dob;
    var dt = dateTime.create();
    var createdDate=dt.format('Y-m-d H:M:S');
    var updateDate=dt.format('Y-m-d H:M:S');
    var encryptPass=mden(password);
    db.f_user_login.count({'email':email},function(err,docs){
        if (docs > 0) {
            var data={"statusCode": 409,"error": "Conflict","message": "A user with this email exist. Please use different email."};
            res.send(data);
        }else{
            var data = {
                email: email,
                password: encryptPass,
                dob:dob,
                created_date:createdDate,
                updated_date:updateDate,
                status:0,
                token:''
            };
            db.f_user_login.save(data,function(err,docs){
                if(!err){
                    if(docs){
                        res.send(docs);
                    }
                }else{
                    console.log('err',err);
                    res.send('could not inserted');
                }
            })
        }
    })
}

Here I am getting the error message through the console . Here I need to insert the data into database.

That error is thrown Satya when you fail to authenticate to a monogDB's db. Do check username and password and try logging in using the shell with those credentials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM