【问题标题】:Getting error while inserting data into MongoDB using Node.js and mongoLAB使用 Node.js 和 mongoLAB 将数据插入 MongoDB 时出错
【发布时间】:2019-07-17 04:54:45
【问题描述】:

我在使用 Node.js 将数据插入 MongoLab 中的 MongoDB 时遇到以下错误。

错误:

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 }

我的代码如下:

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');
                }
            })
        }
    })
}

这里我通过console 收到错误消息。在这里,我需要将数据插入数据库。

【问题讨论】:

  • 我检查了这个链接,找不到任何线索。我正在为 mongodb 使用 MongoLab。
  • 创建用户时,您是否在角色数组中设置了role: "root"
  • 我认为没有。我没有设置,但我可以从现有集合中获取所有值。
  • role=read 存在。

标签: node.js mongodb mlab


【解决方案1】:

当您无法对 monogDB 的数据库进行身份验证时,会引发 Satya 错误。请检查用户名和密码,并尝试使用带有这些凭据的 shell 登录。

【讨论】:

  • 是的,使用相同的 URI 我也可以在其他函数中获取 db 值。
  • 好了,角色刚刚读完。因此不允许写入。
  • 在此处阅读有关角色的更多信息:docs.mongodb.com/v3.2/reference/built-in-roles 并选择您想要的任何一个
猜你喜欢
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2013-10-20
  • 2013-01-02
  • 1970-01-01
  • 2020-01-02
相关资源
最近更新 更多