angularjs - "TypeError: Parameter "本地运行heroku应用程序时的url "must be a string, not undefined"

标签 angularjs node.js git heroku

我遵循了本教程 here ,我没有只将代码托管在 herokus git 上,而是将其托管在 git 上。它可以在heroku应用程序本身上运行,但是当我 pull 出它并使用node server.js在本地运行它时,它给了我下面的错误。

  1. 我已更新 Node

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

  3. 重新制作应用程序。

我怀疑这与 mlab 连接未接通有关。无论如何,非常感谢任何帮助。

C:\College\WebProg4\foodonline>node server.js url.js:88 throw new TypeError('Parameter "url" must be a string, not ' + typeof url); ^

TypeError: Parameter "url" must be a string, not undefined at Url.parse (url.js:88:11) at Object.urlParse [as parse] (url.js:82:5) at module.exports (C:\College\WebProg4\foodonline\node_modules\mongodb\lib\url_parser.js:15:23) at connect (C:\College\WebProg4\foodonline\node_modules\mongodb\lib\mongo_client.js:403:16) at Function.MongoClient.connect (C:\College\WebProg4\foodonline\node_modules\mongodb\lib\mongo_client.js:227:3) at Object. (C:\College\WebProg4\foodonline\server.js:19:21) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at 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);
  });
});

最佳答案

类型错误:参数“url”必须是字符串,而不是未定义

从堆栈跟踪中读取

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

关于angularjs - "TypeError: Parameter "本地运行heroku应用程序时的url "must be a string, not undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458057/

相关文章:

javascript - Angular 防止 mock $http 服务

javascript - 具有原始绑定(bind)的指令中的函数回调

javascript - 如何检查文档/文档快照中是否存在某个字段?

mysql - Node.js mySQL 模块错误

node.js - 如何访问 express 的 uploadDir 属性?

GitKraken 推送失败

javascript - 在 Angular (PhoneGap) 中,构造 AJAX 调用并将返回用作可存储 JSON 数据的最佳方法是什么?

javascript - 在 Angular SPA 中存储凭证

Github 使用 SSH key 提交

git - 如何只公开私有(private) git 存储库的一个文件?