【问题标题】:"TypeError: Parameter "url" must be a string, not undefined" when running heroku app locally“TypeError:参数“url”必须是字符串,而不是未定义“在本地运行heroku应用程序时
【发布时间】:2017-09-13 11:01:39
【问题描述】:

我按照本教程 here 进行操作,而不是仅将代码托管在 herokus git 上,而是将其托管在 git 上。它适用于应用程序本身的 heroku,但是当我使用 node server.js 将其拉出并在本地运行时,它给了我以下错误。

  1. 我已经更新了节点

  2. 使用 heroku localheroku local web 运行它,同样的事情...

  3. 重新打造应用。

我怀疑这与 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


【解决方案1】:

TypeError: Parameter "url" must be a string, not undefined

从堆栈跟踪中读取

`at Function.MongoClient.connect(...\node_modules\mongodb\lib\mongo_client.js:227:3)
at Object.<anonymous> (......\server.js:19:21)`

process.env.MONGODB_URI 未定义或无效。

您可以在尝试启动服务器之前添加简单的检查,以检查所有可能未定义或无效的必需参数。看看https://blog.risingstack.com/10-best-practices-for-writing-node-js-rest-apis/关于配置的处理方式。

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 2017-12-04
    • 2018-05-16
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2023-02-18
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多