Home > Enterprise >  Trying to connect to Heroku: MongoParseError: URI malformedm cannot be parsed
Trying to connect to Heroku: MongoParseError: URI malformedm cannot be parsed

Time:02-14

I'm trying to post my app to Heroku for class and it keeps crashing. This is the error code I'm receiving. I'm not exactly sure what's happening, the H10 error is a blanket statement, maybe someone here could shed some light. This is what I'm getting when using heroku logs --tail. Edit: My app works totally fine on my local machine, I'm only getting this error when trying to post to Heroku

2022-02-13T18:27:21.000000 00:00 app[api]: Build started by user [email protected]
2022-02-13T18:27:38.841499 00:00 app[api]: Release v5 created by user [email protected]
2022-02-13T18:27:38.841499 00:00 app[api]: Deploy 7d112609 by user [email protected]
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]: > [email protected] 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

This is my server.js file

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}!`);
});

CodePudding user response:

Glancing at your log file, you can see that the top of the stack (at the point in which the exception is thrown) points to a parseConnectionString call. As you most likely deduced (since you included this in your title), the line above is a big clue as to what is going on:

MongoParseError: URI malformed, cannot be parsed

This indicates an issue with the connection string you are providing to the mongoose.connect function. Given the code you have shared, this can only be the result of an invalid process.env.MONGODB_URI value or the fallback mongodb://localhost/budget. Depending on how the MongoDB instance was provisioned, I might lean towards checking the value of the environment variable first, but if you are unsure about this, it might be best to first determine which of the two values your application is using (i.e., you could log this).

Overall, the issue appears to be related to the connection string you are using to connect to the MongoDB instance, and not explicitly caused by an issue with your application.

CodePudding user response:

Hello and thank you everyone for helping. My issue was that I was using my Mongo username/password login info in the URI string, instead of my Mongo username/password combination. I'm not sure if that makes any sense but I have it up and running. Thank you again!

  • Related