简体   繁体   中英

Mongoose connected to mongoDB but have not initialized the database

My connection code:

const mongoose = require("mongoose");

//ES6 Promise
mongoose.Promise = global.Promise;

//connect to mongoDB
mongoose.connect("mongodb://localhost/smslist", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

mongoose.connection
  .once("open", function () {
    console.log("Connected to database successfuly");
  })
  .on("error", function (error) {
    console.log(`DB Connection error:`, error);
  });

The mongod server terminal shows that the connection is successfull

2020-04-27T15:35:24.500+0300 I  NETWORK  [listener] connection accepted from 127.0.0.1:61286 #1 (1 connection now open)
2020-04-27T15:35:24.506+0300 I  NETWORK  [conn1] received client metadata from 127.0.0.1:61286 conn1: { driver: { name: "nodejs|Mongoose", version: "3.5.6" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.18362" }, platform: "'Node.js v12.16.2, LE (unified)", version: "3.5.6|5.9.10" }

The node terminal also prints

Connected to database successfuly

But when I try to use a mongoDB UI like robomongo/studio 3t/mongodb compass community, I will not see a database named smslist as expected. I also realized that if I close the db server terminal the node terminal still shows connected to database successfully and not DB Connection error: , error as expected.

any idea?

If you haven't created any Model yet in this "smslist" database then you can not see any thing about this

You need to create some Model for this "smslist" database then you can see this in your db.

I agree it depends on the code that follows the above. I've been working through a tutorial on this.

Here is the code and my notes on what finally worked.

// from scratch tutorial 
// WORKS!!!!
//require mongoose - works
   const mongoose = require('mongoose');

//define constant - works const url = 'mongodb://127.0.0.1:27017/fruit'

// connect to datbase works as evidenced by message
   mongoose.connect(url, { useNewUrlParser: true });
  const db = mongoose.connection
  db.once('open', _ => {
   console.log('Database connected:', url)
});

db.on('error', err => {
console.error('connection error:', err)
});

//above works!! but do not see new database in mongodb

//now add schema
const Schema = mongoose.Schema

// define schema
fruitSchema = new Schema({
 name: String,
 rating: Number,
review: String
});

// create the model - or "collection" use singular - mongoose makes it plural 
  // inside database
   const Fruit = mongoose.model("Fruit", fruitSchema);

 //now create the document from the model (note upper and lower case)
const fruit = new Fruit({
  name: "Pear",
  rating: 10,
  review: "good with Brie"
});

fruit.save();

  //close the connection works but causes an error when trying to add a document
// mongoose.connection.close();

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