使用Node.js连接到MongoLab中的mongoDB时出错

时间:2017-07-31 16:48:03

标签: javascript node.js mlab mongojs

使用node.js连接到MongoLab中的mongoDB时出错。错误解释如下。

您的环境已设置为使用Node.js 6.9.1(x64)和npm。

C:\Users\User>cd C:\xampp\htdocs\fgd

C:\xampp\htdocs\fgd>node server
Server is running on the port8989
GET / 200 113.510 ms - 6352
GET /calendar/tcal.js 404 7.320 ms - 29
GET /calendar/tcal.js 404 1.024 ms - 29
GET /favicon.ico 404 0.845 ms - 24
passstring
Error: Username containing an unescaped at-sign
    at module.exports (C:\xampp\htdocs\fgd\node_modules\mongodb\lib\url_parser.j
s:59:13)
    at connect (C:\xampp\htdocs\fgd\node_modules\mongodb\lib\mongo_client.js:289
:16)
    at Function.MongoClient.connect (C:\xampp\htdocs\fgd\node_modules\mongodb\li
b\mongo_client.js:113:3)
    at C:\xampp\htdocs\fgd\node_modules\mongojs\lib\database.js:29:15
    at run (C:\xampp\htdocs\fgd\node_modules\thunky\index.js:13:3)
    at Collection._getConnection (C:\xampp\htdocs\fgd\node_modules\thunky\index.
js:27:3)
    at Collection._getCollection (C:\xampp\htdocs\fgd\node_modules\mongojs\lib\c
ollection.js:17:10)
    at getCursor (C:\xampp\htdocs\fgd\node_modules\mongojs\lib\collection.js:32:
10)
    at C:\xampp\htdocs\fgd\node_modules\mongojs\lib\cursor.js:12:5
    at run (C:\xampp\htdocs\fgd\node_modules\thunky\index.js:13:3)
POST /login 500 68.758 ms - 1001

我正在解释下面的代码。

var mongoJs=require('mongojs');
var CryptoJS = require("crypto-js");
var database='FGDP';
var collections=['f_users'];
var MONGOLAB_URI="mongodb://subhra:******123@@ds127153.mlab.com:27153/fgdp";
var db=mongoJs(MONGOLAB_URI,collections);
var ses;
db.on('connect', function () {
    console.log('database connected')
});

1 个答案:

答案 0 :(得分:0)

只需使用“ mongodb://用户名:” + encodeURIComponent(“ p @ ssword”)+“ @ localhost:27017 / database”