node js server没有启动字符串错误

时间:2018-03-01 13:33:01

标签: node.js

TypeError:参数" url"必须是一个字符串,而不是未定义     在Url.parse(url.js:103:11)     at Object.urlParse [as parse](url.js:97:13)     在module.exports(C:\ Users \ Admin \ Desktop \ Innov \ node_modules \ mongodb \ lib \ url_parser.js:13:23)     在Promise(C:\ Users \ Admin \ Desktop \ Innov \ node_modules \ mongoose \ lib \ connection.js:332:5)     在新的承诺()     在NativeConnection.Connection.openUri(C:\ Users \ Admin \ Desktop \ Innov \ node_modules \ mongoose \ lib \ connection.js:331:19)     在Mongoose.connect(C:\ Users \ Admin \ Desktop \ Innov \ node_modules \ mongoose \ lib \ index.js:207:15)     在对象。 (C:\用户\管理员\桌面\伊诺斯\ server.js:8:10)     在Module._compile(module.js:643:30)     at Object.Module._extensions..js(module.js:654:10)     在Module.load(module.js:556:32)     在tryModuleLoad(module.js:499:12)     在Function.Module._load(module.js:491:3)     在Function.Module.runMain(module.js:684:10)     在启动时(bootstrap_node.js:187:16)     在bootstrap_node.js:608:3

1 个答案:

答案 0 :(得分:1)

我认为这是因为您的配置模块(数据库字段)上有额外的空间:

module.export = {
    "database": " mongodb://<dbuser>:<dbpassword>@ds251988.mlab.com:51988/xxxxx",
    "port": process.env.PORT || 3000,
    "secretKey": "xxxx"
};

应替换为:

module.export = {
    "database": "mongodb://<dbuser>:<dbpassword>@ds251988.mlab.com:51988/xxxxx",
    "port": process.env.PORT || 3000,
    "secretKey": "xxxx"
};

编辑1

如果它仍然不起作用,也许您应该像这样手动设置主机名:

 module.export = {
        "database": "mongodb://localhost:27017/<dbuser>:<dbpassword>@ds251988.mlab.com:51988/xxxxx",
        "port": process.env.PORT || 3000,
        "secretKey": "xxxx"
    };

编辑2

这就是我进行数据库连接的方式:

var mongoose = require('mongoose');
var connectionString = 'mongodb://127.0.0.1:27017/myDatabase';
var options = {
    auth: {authdb: 'myDatabase'},
    user: 'myUser',
    pass: 'myPassword'
};

mongoose.connect(connectionString, options);
mongoose.connection.once('connected', function (err) {
    console.log(err || "Connected to database");
}).on('error', function (err) {
    console.log('Database Error : ', err);
}).on('disconnected', function () {
    console.log('Disconnected from database !');
});

希望它有所帮助。