Trying to connect to Heroku: MongoParseError: URI malformedm cannot be parsed
Trying to connect to Heroku: MongoParseError: URI malformedm cannot be parsed
我正在尝试 post 我的应用程序到 Heroku for class 但它总是崩溃。这是我收到的错误代码。我不确定发生了什么,H10 错误是笼统的声明,也许这里有人可以阐明一些问题。这就是我在使用 heroku logs --tail 时得到的结果。
编辑:我的应用程序在我的本地机器上运行良好,我只在尝试 post 到 Heroku
时收到此错误
2022-02-13T18:27:21.000000+00:00 app[api]: Build started by user nmendez6594@gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Release v5 created by user nmendez6594@gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Deploy 7d112609 by user nmendez6594@gmail.com
2022-02-13T18:27:39.000000+00:00 app[api]: Build succeeded
2022-02-13T18:27:39.104174+00:00 heroku[web.1]: State changed from crashed to starting
2022-02-13T18:27:40.901836+00:00 heroku[web.1]: Starting process with command `npm start`
2022-02-13T18:27:42.058731+00:00 app[web.1]:
2022-02-13T18:27:42.058745+00:00 app[web.1]: > budget-app@1.0.0 start
2022-02-13T18:27:42.058746+00:00 app[web.1]: > node server.js
2022-02-13T18:27:42.058746+00:00 app[web.1]:
2022-02-13T18:27:42.395832+00:00 app[web.1]: App running on port 55680!
2022-02-13T18:27:42.401378+00:00 app[web.1]: /app/node_modules/mongodb/lib/core/uri_parser.js:580
2022-02-13T18:27:42.401379+00:00 app[web.1]: return callback(new MongoParseError('URI malformed, cannot be parsed'));
2022-02-13T18:27:42.401380+00:00 app[web.1]: ^
2022-02-13T18:27:42.401380+00:00 app[web.1]:
2022-02-13T18:27:42.401380+00:00 app[web.1]: MongoParseError: URI malformed, cannot be parsed
2022-02-13T18:27:42.401381+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:580:21)
2022-02-13T18:27:42.401381+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/connect.js:283:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:284:5
2022-02-13T18:27:42.401382+00:00 app[web.1]: at maybePromise (/app/node_modules/mongodb/lib/utils.js:692:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:280:10)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:836:12
2022-02-13T18:27:42.401383+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401383+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:832:19)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:351:10
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-02-13T18:27:42.401384+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1149:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:350:20)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:19:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-02-13T18:27:42.521294+00:00 heroku[web.1]: Process exited with status 1
2022-02-13T18:27:42.598710+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-13T18:27:48.281004+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=budget-app-pwa4.herokuapp.com request_id=5de2651b-5f3f-4a1e-84f2-18c0123b8dd7 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
2022-02-13T18:27:49.746492+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=budget-app-pwa4.herokuapp.com request_id=f15d0dc1-7a19-49e1-a743-13f31cac6c31 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
这是我的 server.js 文件
const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");
const compression = require("compression");
const PORT = process.env.PORT || 3001;
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/budget";
const app = express();
app.use(logger("dev"));
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// routes
app.use(require("./routes/api.js"));
app.listen(PORT, () => {
console.log(`App running on port ${PORT}!`);
});
查看您的日志文件,您可以看到堆栈顶部(在抛出异常的位置)指向 parseConnectionString 调用。正如您最有可能推断的那样(因为您将其包含在标题中),上面的行是关于正在发生的事情的重要线索:
MongoParseError: URI malformed, cannot be parsed
这表示您提供给 mongoose.connect 函数的连接字符串存在问题。鉴于您共享的代码,这只能是无效 process.env.MONGODB_URI 值或回退 mongodb:/[=25 的结果=]。根据 MongoDB 实例的配置方式,我可能倾向于首先检查环境变量的值,但如果您不确定这一点,最好先确定您的应用程序正在使用的两个值中的哪一个(即,您可以记录它)。
总体而言,该问题似乎与您用于连接到 MongoDB 实例的连接字符串有关,而不是由您的应用程序问题明确引起的。
您好,感谢大家的帮助。我的问题是我在 URI 字符串中使用了我的 Mongo username/password 登录信息,而不是我的 Mongo 数据库 username/password 组合。我不确定这是否有意义,但我已经准备好 运行。再次感谢!
我正在尝试 post 我的应用程序到 Heroku for class 但它总是崩溃。这是我收到的错误代码。我不确定发生了什么,H10 错误是笼统的声明,也许这里有人可以阐明一些问题。这就是我在使用 heroku logs --tail 时得到的结果。 编辑:我的应用程序在我的本地机器上运行良好,我只在尝试 post 到 Heroku
时收到此错误2022-02-13T18:27:21.000000+00:00 app[api]: Build started by user nmendez6594@gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Release v5 created by user nmendez6594@gmail.com
2022-02-13T18:27:38.841499+00:00 app[api]: Deploy 7d112609 by user nmendez6594@gmail.com
2022-02-13T18:27:39.000000+00:00 app[api]: Build succeeded
2022-02-13T18:27:39.104174+00:00 heroku[web.1]: State changed from crashed to starting
2022-02-13T18:27:40.901836+00:00 heroku[web.1]: Starting process with command `npm start`
2022-02-13T18:27:42.058731+00:00 app[web.1]:
2022-02-13T18:27:42.058745+00:00 app[web.1]: > budget-app@1.0.0 start
2022-02-13T18:27:42.058746+00:00 app[web.1]: > node server.js
2022-02-13T18:27:42.058746+00:00 app[web.1]:
2022-02-13T18:27:42.395832+00:00 app[web.1]: App running on port 55680!
2022-02-13T18:27:42.401378+00:00 app[web.1]: /app/node_modules/mongodb/lib/core/uri_parser.js:580
2022-02-13T18:27:42.401379+00:00 app[web.1]: return callback(new MongoParseError('URI malformed, cannot be parsed'));
2022-02-13T18:27:42.401380+00:00 app[web.1]: ^
2022-02-13T18:27:42.401380+00:00 app[web.1]:
2022-02-13T18:27:42.401380+00:00 app[web.1]: MongoParseError: URI malformed, cannot be parsed
2022-02-13T18:27:42.401381+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb/lib/core/uri_parser.js:580:21)
2022-02-13T18:27:42.401381+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/connect.js:283:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongodb/lib/mongo_client.js:284:5
2022-02-13T18:27:42.401382+00:00 app[web.1]: at maybePromise (/app/node_modules/mongodb/lib/utils.js:692:3)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:280:10)
2022-02-13T18:27:42.401382+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:836:12
2022-02-13T18:27:42.401383+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401383+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:832:19)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:351:10
2022-02-13T18:27:42.401384+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-02-13T18:27:42.401384+00:00 app[web.1]: at new Promise (<anonymous>)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-02-13T18:27:42.401384+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1149:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:350:20)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:19:10)
2022-02-13T18:27:42.401385+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-02-13T18:27:42.521294+00:00 heroku[web.1]: Process exited with status 1
2022-02-13T18:27:42.598710+00:00 heroku[web.1]: State changed from starting to crashed
2022-02-13T18:27:48.281004+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=budget-app-pwa4.herokuapp.com request_id=5de2651b-5f3f-4a1e-84f2-18c0123b8dd7 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
2022-02-13T18:27:49.746492+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=budget-app-pwa4.herokuapp.com request_id=f15d0dc1-7a19-49e1-a743-13f31cac6c31 fwd="32.215.142.46" dyno= connect= service= status=503 bytes= protocol=https
这是我的 server.js 文件
const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");
const compression = require("compression");
const PORT = process.env.PORT || 3001;
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/budget";
const app = express();
app.use(logger("dev"));
app.use(compression());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
// routes
app.use(require("./routes/api.js"));
app.listen(PORT, () => {
console.log(`App running on port ${PORT}!`);
});
查看您的日志文件,您可以看到堆栈顶部(在抛出异常的位置)指向 parseConnectionString 调用。正如您最有可能推断的那样(因为您将其包含在标题中),上面的行是关于正在发生的事情的重要线索:
MongoParseError: URI malformed, cannot be parsed
这表示您提供给 mongoose.connect 函数的连接字符串存在问题。鉴于您共享的代码,这只能是无效 process.env.MONGODB_URI 值或回退 mongodb:/[=25 的结果=]。根据 MongoDB 实例的配置方式,我可能倾向于首先检查环境变量的值,但如果您不确定这一点,最好先确定您的应用程序正在使用的两个值中的哪一个(即,您可以记录它)。
总体而言,该问题似乎与您用于连接到 MongoDB 实例的连接字符串有关,而不是由您的应用程序问题明确引起的。
您好,感谢大家的帮助。我的问题是我在 URI 字符串中使用了我的 Mongo username/password 登录信息,而不是我的 Mongo 数据库 username/password 组合。我不确定这是否有意义,但我已经准备好 运行。再次感谢!