Asis Datta
Asis Datta

Reputation: 563

Not able to find mongoose models after creation

I have created one express app and the database connection .

var app = express();
app.db = mongoose.connect("mongodb://localhost/forcast-db");

var accountSchema = new mongoose.Schema({
            accountId:{
                type:String,
                required:true
            }
        });
app.db.model('Account', accountSchema);
console.log(app.db.models);

But it is printing a empty object.

used library "express": "^4.13.4", "mongoose": "^4.4.7",

Any kind of help will be highly appreciated.

Upvotes: 0

Views: 110

Answers (2)

aitnasser
aitnasser

Reputation: 1256

to get all models created with your mongoose connection use app.db.connection.models so your code should be like this:

    var app = express();
    app.db = mongoose.connect("mongodb://localhost/forcast-db");

    var accountSchema = new mongoose.Schema({
                accountId:{
                    type:String,
                    required:true
                }
            });
   var accountModel= app.db.model('Account', accountSchema);
    console.log(app.db.connection.models);
   //to create a document from this Account model and save it
    var newAccount= new accountModel({accountId:'1111'});
    newAccount.save();

Upvotes: 1

Durgaprasad Budhwani
Durgaprasad Budhwani

Reputation: 977

For list of models use console.log(mongoose.models); instead of console.log(app.db.models);. console.log(app.db.models); will not give you list of model created.

Durga

Upvotes: 0

Related Questions