Mongoose connection is null on schema

97 views
Skip to first unread message

dev0x10

unread,
Aug 20, 2013, 12:31:46 PM8/20/13
to mongoo...@googlegroups.com
I've read some articles about how to write the proper way for open mongoose connections. On the articles it opens Mongoose connection on app start and close it when app close.
But I don't know why my code is not working when I open the connection on app start.

//app.js
var Hapi = require('hapi'),
    config = require("./config"),
    mongoose = require("mongoose");

mongoose.connect(config.mongo.getConnectionString());
.... (start server codes)


One of my schema, I have several schemas.
//Schema file
"use strict";

var mongoose = require('mongoose');


var instructorSchema = require("mongoose").Schema({
    fname: {
        type: String
    },
    lname: {
        type: String
    },
    email: {
        type: String
    },
    password: {
        type: String
    },
    courses: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Course'
    },
    registeredDate: {
        type: Date
    }
});

module.exports = mongoose.model('Instructor', instructorSchema);

// file where I use mongoose schema
"use strict";

var mongoose = require('mongoose'),
    instructorSchema = require("../schemas/instructorSchema"),
    instructorModel = mongoose.model("Instructor");

function Instructor() {}

Instructor.prototype = {
    create: function (instructorData, callback) {
        var instructor = new instructorModel(instructorData);
        instructor.save(function () {
                callback(instructor._id);
        });
    },

    get: function (instructorId, callback) {
        instructorModel.findById(instructorId, function (err, result) {
                callback(result);
        });
    },

    getByData: function(instructorData, callback) {
        instructorModel.find(instructorData, function (err, instructor) {
                callback(instructor);
        });
    },

    delete: function (instructorId, callback) {
        instructorModel.remove({"_id": instructorId}, function (err, result) {
                callback(result);
        });
    }
};

module.exports = Instructor;

dev0x10

unread,
Aug 20, 2013, 12:34:20 PM8/20/13
to mongoo...@googlegroups.com
The problem is when I try to insert, the program stop without any error. I think it's because the connection is not open yet when schema file try to register the model.
How to write the codes properly?

THanks

Aaron Heckmann

unread,
Aug 20, 2013, 7:31:31 PM8/20/13
to mongoo...@googlegroups.com
Hard to tell from what you've posted. Try adding an error handler `mongoose.connection.on('error', console.log)`. 

BTW, mongoose buffers commands until the connection opens and then executes them all.


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Aaron


dev0x10

unread,
Aug 20, 2013, 11:04:57 PM8/20/13
to mongoo...@googlegroups.com
I think the connection is not open. I've put error handler like you said but nothing out.
Btw, I'm running a test case with mocha. It always timeout no matter how big I set the mocha's timeout.
Is the something wrong with the codes above? I mean the way I open connection and declare a model?

dev0x10

unread,
Aug 20, 2013, 11:35:44 PM8/20/13
to mongoo...@googlegroups.com
I think I found the solution here https://groups.google.com/d/msg/mongoose-orm/PXTjqqpaDFk/rlYHsIN9ACwJ
Same problem

Thank You Aaron :D
Reply all
Reply to author
Forward
0 new messages