【发布时间】:2017-09-13 11:01:39
【问题描述】:
我按照本教程 here 进行操作,而不是仅将代码托管在 herokus git 上,而是将其托管在 git 上。它适用于应用程序本身的 heroku,但是当我使用 node server.js 将其拉出并在本地运行时,它给了我以下错误。
我已经更新了节点
使用 heroku local 和 heroku local web 运行它,同样的事情...
- 重新打造应用。
我怀疑这与 mlab 连接未通过有关。无论如何,任何帮助都非常感谢。
C:\College\WebProg4\foodonline>node server.js url.js:88 throw new TypeError('Parameter "url" 必须是字符串,而不是' + typeof url); ^
TypeError: 参数“url”必须是字符串,不是未定义的 在 Url.parse (url.js:88:11) 在 Object.urlParse [解析] (url.js:82:5) 在 module.exports (C:\College\WebProg4\foodonline\node_modules\mongodb\lib\url_parser.js:15:23) 在连接时(C:\College\WebProg4\foodonline\node_modules\mongodb\lib\mongo_client.js:403:16) 在 Function.MongoClient.connect (C:\College\WebProg4\foodonline\node_modules\mongodb\lib\mongo_client.js:227:3) 在对象。 (C:\College\WebProg4\foodonline\server.js:19:21) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12)
Server.JS
var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var CONTACTS_COLLECTION = "contacts";
var app = express();
app.use(bodyParser.json());
// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;
// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
// Save database object from the callback for reuse.
db = database;
console.log("Database connection ready");
// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
【问题讨论】:
-
无需重新开始堆栈跟踪报告
server.js发生错误的行19:21,可能是一个没有值的变量..undefined..尝试从@987654326 发布一些代码@ -
@Mr.Phoenix 是的,我在那里添加了它。
-
process.env.MONGODB_URI没有设置或者无效,看看环境变量。 -
@Mr.Phoenix 是的,你是对的,我将 process.env.MONGODB_URI 更改为正确的 url。谢谢!
标签: angularjs node.js git heroku