Search code examples
node.jsheroku

Application Error while uploading to Heroku


For my application's backend I'm trying to host in on heroku. After creating a new app on heroku I've followed all the steps that it showed me and it was deployed successfully. But when I go to the link it gives me application error. I tried to get the log by heroku logs --tail and it gave me this

2021-12-29T07:57:12.741895+00:00 app[web.1]: Error:  The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2021-12-29T07:57:12.751654+00:00 app[web.1]: [nodemon] clean exit - waiting for changes before restart
2021-12-29T07:58:11.201323+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-12-29T07:58:11.236815+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-12-29T07:58:11.259774+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2021-12-29T07:58:11.400798+00:00 heroku[web.1]: Process exited with status 22
2021-12-29T07:58:11.473743+00:00 heroku[web.1]: State changed from starting to crashed
2021-12-29T08:56:18.088383+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pin-map-app.herokuapp.com request_id=30848095-cca3-4afe-b1ac-705529a0b023 fwd="103.166.88.18" dyno= connect= service= status=503 bytes= protocol=https
2021-12-29T08:56:19.043794+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pin-map-app.herokuapp.com request_id=dedb7d05-a475-4e11-972d-e484b397c88e fwd="103.166.88.18" dyno= connect= service= status=503 bytes= protocol=https

As you can see there's something with the openUri() problem and for this I had already set up dotenv and added dotenv.config() in my index.js. I tried heroku restart and pushed all the codes again but the problem remains same. index.js

import express from "express";
import mongoose from "mongoose";
import pinRoutes from "./routes/pins.js";
import userRoutes from "./routes/users.js";
import cors from "cors";
import dotenv from "dotenv";

dotenv.config();
const app = express();
app.use(express.json({ limit: "30mb", extended: true }));
app.use(cors());
app.use("/pins", pinRoutes);
app.use("/user", userRoutes);

const PORT = process.env.PORT || 5000;

app.get("/", (req, res) => {
  res.send("welcome to pin it");
});

mongoose
  .connect(process.env.CONNECTION_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => {
      console.log(`Server started on port ${PORT}`);
    });
  })
  .catch((err) => {
    console.log("Error: ", err.message);
  });

In my .env file I only have the CONNECTION_URL and nothing else. I had deployed some other apps before but this is the first time I'm getting this type of error.


Solution

  • Firstly: make sure that all the independence is installed in package.json

    Secondly:

    Despite your setup the correct environment, that shows you the mongoose error tell that something is wrong in your first parameter, try to make direct linking, without .env

    just make sure that's the port which connected in it is:

    var port = normalizePort(process.env.PORT || '3000');
    app.set('port', port);
    
    this just makes the port that connected dynamic
    If port 3000 does not work will switch to another one.  
    

    And listen it as:

    app.listen(port)