" TypeError:参数" url"必须是一个字符串,而不是未定义的"在本地运行heroku应用程序时

时间:2017-04-17 19:19:03

标签: angularjs node.js git heroku

我遵循了本教程here,而不是仅仅托管herokus git上的代码我在git上托管了它。它适用于应用程序本身的heroku但是当我使用节点server.js 在本地运行它时它会给我以下错误。

  1. 我已更新节点

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

  3. 重新制作应用程序。
  4. 我怀疑它与mlab连接无关。无论如何,任何帮助都非常感激。

      

    C:\ College \ WebProg4 \ foodonline>节点server.js       url.js:88           抛出新的TypeError('参数" url"必须是字符串,而不是' + typeof url);           ^

         

    TypeError:参数" url"必须是一个字符串,而不是未定义           在Url.parse(url.js:88:11)           at Object.urlParse [as parse](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:\学院\ 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);
      });
    });
    

1 个答案:

答案 0 :(得分:3)

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/有关处理配置的方式。