Home > Software engineering >  mongoose is not creating db using mongoose.connect
mongoose is not creating db using mongoose.connect

Time:11-22

I'm trying to create new database using mongoose.connect('mongodb://localhost:27017/imagesDB'); But is not creating. And please don't answer me saying that I need to add entries to it. I did it. Here's my full code.

const express = require("express");
const mongoose = require('mongoose');

const app = express();

mongoose.connect('mongodb://localhost:27017/imagesDB').then(() => {
console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

const imageSchema = new mongoose.Schema ({
    name: String,
    category: String,
    author: String,
    tag: String,
    imageURL: String
});

const Image = mongoose.model("Image", imageSchema);

const image = new Image({
    name: "Cat",
    category: "Animal",
    author:"Cat",
    tag: "Animals",
    imageURL: "https://cdn-media-2.freecodecamp.org/w1280/5f9c9a5d740569d1a4ca2531.jpg"
});

image.save();

app.get("/", function(req, res){
    res.send("Everything is running properly!");
});

app.listen(3000, function(){
    console.log("Server succesfully started on port 3000!");
});

Can you guys please test it for yourself? Does this creates a new database called imagesDB???

For me no, It is not working. I opened new tab in terminal and commanded mongo so it opened mongo client where i typed show dbs but it is not showing except these three admin, config and local.

I also wanna make it clear that mongo service is running, I did it by commanding mongod --ipv6 in terminal.

CodePudding user response:

Are you using Mongoose Compass or Atlas? Secondly, try doing it on VSCode terminal it will work then

CodePudding user response:

Have you created a database named "imagesDB" in MongoDB Compass?

  • Related