SajjanKarn / cms-youtube-backend

backend for cms created for youtube.
3 stars 3 forks source link

Need some assistance #1

Open MDoughlin opened 1 year ago

MDoughlin commented 1 year ago

I am setting up the backend and I am running into the issue where I am not getting the message "connection to database established" but I am not getting an error either.

I have double checked my code and everything looks good

My Db.js

const mongoose = require("mongoose");

const connectDB = async () => {
  return mongoose
    .connect("mongodb://localhost/contact_mern")
    .then(() => console.log(`conection to database established ...`))
    .catch((error) => console.log(error));
};

module.exports = connectDB;

My app.js

const express = require('express');
const morgan = require('morgan');

const connectDB = require('./config/db');

const app = express();

//middleware
app.use(express.json());
app.use(morgan("tiny"));

//routes
app.get("/", (req, res) => {
  res.send("Hello World");
});

//server config
const PORT = process.env.PORT || 8000;
app.listen(PORT, async () => {
  await connectDB;
  console.log(`server listening on port ${PORT}`);
});

I also typed mongod into my terminal and the mongodb server didn't start

SajjanKarn commented 1 year ago

You missed to call the function

It should be await connectDB();

On Sun, Sep 17, 2023, 9:57 AM MDoughlin @.***> wrote:

I am setting up the backend and I am running into the issue where I am not getting the message "connection to database established" but I am not getting an error either.

I have double checked my code and everything looks good

My Db.js

const mongoose = require("mongoose"); const connectDB = async () => { return mongoose .connect("mongodb://localhost/contact_mern") .then(() => console.log(conection to database established ...)) .catch((error) => console.log(error));}; module.exports = connectDB;

My app.js

const express = require('express');const morgan = require('morgan'); const connectDB = require('./config/db'); const app = express(); //middlewareapp.use(express.json());app.use(morgan("tiny")); //routesapp.get("/", (req, res) => { res.send("Hello World");}); //server configconst port = process.env.PORT || 8000;app.listen(port, async () => { await connectDB; console.log(server listening on port ${port});});

— Reply to this email directly, view it on GitHub https://github.com/SajjanKarn/cms-youtube-backend/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALYMIEGQZSA5CJLFQQMOZJTX2Z2DRANCNFSM6AAAAAA43K5NHI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

MDoughlin commented 1 year ago

I do have await connectDB above where I am configuring the server

SajjanKarn commented 1 year ago

You missed the paranthesis ();

On Sun, Sep 17, 2023, 10:16 AM MDoughlin @.***> wrote:

I do have await connectDB above where I am configuring the server

— Reply to this email directly, view it on GitHub https://github.com/SajjanKarn/cms-youtube-backend/issues/1#issuecomment-1722389049, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALYMIEDCXNHW4JQ7PP6LRJ3X2Z4IHANCNFSM6AAAAAA43K5NHI . You are receiving this because you commented.Message ID: @.***>