Rekepaw
Rekepaw

Reputation: 53

MongoError: database names cannot contain the character '/'

I am using moongoose npm package. When I try to connect with mongodb I am getting this error. Here is my code.

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1/27017/task-manager-api", {
  useNewUrlParser: true,
  useCreateIndex: true,
});

const User = mongoose.model("User", {
  name: {
    type: String,
  },
  age: {
    type: Number,
  },
});

const me = new User({
  name: "Sushant",
  age: 17,
});

me.save()
  .then(() => {
    console.log(me);
  })
  .catch((error) => {
    console.log("Error Spotted");
  });

MongoError: database names cannot contain the character '/'

Upvotes: 2

Views: 1204

Answers (2)

ale917k
ale917k

Reputation: 1768

Your db URI contains a small typo mistake, as you wrote: mongodb://127.0.0.1/27017/task-manager-api

While it should be: mongodb://127.0.0.1:27017/task-manager-api

Upvotes: 0

Shafqat Jamil Khan
Shafqat Jamil Khan

Reputation: 1037

Port number should be added with colon instead of forward slash

const mongoose = require("mongoose");
    
    mongoose.connect("mongodb://127.0.0.1:27017/task-manager-api", {
      useNewUrlParser: true,
      useCreateIndex: true,
    });
    
    const User = mongoose.model("User", {
      name: {
        type: String,
      },
      age: {
        type: Number,
      },
    });
    
    const me = new User({
      name: "Sushant",
      age: 17,
    });
    
    me.save()
      .then(() => {
        console.log(me);
      })
      .catch((error) => {
        console.log("Error Spotted");
      });

Upvotes: 1

Related Questions