简体   繁体   中英

mongoDB is not working after I deployed my MERN app using Heroku, while the app works fine on my localhost

I am pulling my hair for couple of hours suffering from this issue... :(

The app works fine on my localhost, but mongoDB doesn't seem to be connected to my app.

It keeps gives me a "Type error" and I think it does Not GET a response from mongoDB.

Here's my index.js file in backend directory, and.env file is in the same directory with index.js.


// backend/index.js

const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const app = express();
const pinRoute = require("./routes/pins");
const userRoute = require("./routes/users");
const path = require("path")

dotenv.config();

app.use(express.json());
const uri = process.env.MONGO_URL;
console.log(uri)

// console.log does show uri.

mongoose
  .connect(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
  })
  .then(() => {
    console.log("MongoDB Connected :)");
  })
  .catch((err) => console.log(err));

app.use("/api/pins", pinRoute);
app.use("/api/users", userRoute);

if (process.env.NODE_ENV === "production") {
  app.use(express.static("frontend/build"));

  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "../frontend", "build", "index.html"))
  })
}

const port = process.env.PORT || 8800;

app.listen(port, () => {
  console.log(`Backend server is running at ${port} :)`);
});

and here's my package.json


// this is in the root directory.

{
  "name": "backend",
  "version": "1.0.0",
  "main": "backend/index.js",
  "license": "MIT",
  "engines": {
    "node": "14.15.4",
    "yarn": "1.22.10"
  },
  "scripts": {
    "start": "node backend/index.js",
    "backend": "nodemon backend/index.js",
    "frontend": "cd frontend && yarn start",
    "heroku-postbuild": "cd frontend && yarn install && yarn build ",
    "build": "cd frontend && yarn build"
  },
  "dependencies": {
    "bcrypt": "^5.0.1",
    "dotenv": "^9.0.2",
    "express": "^4.17.1",
    "mongoose": "^5.12.8",
    "nodemon": "^2.0.7"
  }
}

This is the error I get...

1.Try using the environment variable name as a parameter to mongoose.connect and see if it works. You do not need to assign the value to the uri constant as you did so.

    mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
  })
  .then(() => {
    console.log("MongoDB Connected);
  })
  .catch((err) => console.log(err));
  1. You could add a route to your base URL that gives you a response when you hit the same.

     app.get("/", (req, res) => { res.send("Default Route Works;"); })

EDIT:

Add this to your app.js below the middleware and whenever you redirect to

http://localhost:PORT/
eg. http://localhost:8080/    GET Request

you should see the message "Default Route Works." on the screen, Alternatively you can send a GET request to the same end point via an API client like Insomnia. or POSTMAN.

Note: Please change your port number to where it says PORT in the localhost address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM