我在提交表单时遇到错误(错误请求:400错误)。
{name:“MongoError”,connectionId:75476,
错误:“未授权在database-name.shipper上插入”,...}
代码:13
的ConnectionId:75476
错误:“未授权在database-name.shipper上插入”
N:0
名: “MongoError”
确定:1
设置为在本地Mongo数据库中记录数据时工作正常,我可以看到数据,但是当我使用mlab时,当我尝试提交表单来记录信息时,我在控制台中收到此错误在数据库中。当我使用nodemon app.js启动我的应用程序时,我得到: Express服务器在端口80上侦听 mongo ::连接数据库::“database-name”
这是我在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');
我一直在寻找解决方案,但还没有找到解决方案。任何帮助或任何能够带领我朝着正确方向前进的人都将不胜感激!