Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
mongoose is not creating db using mongoose.connect

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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You have to created your database named "imagesDB" in MongoDB Compass, or other GUI tool.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is solved by changing localhost to 127.0.0.1 .

Here's how it looks

mongoose.connect('mongodb://127.0.0.1:27017/imagesDB').then(() => {
console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda