3

I'm creating an a terminal application that uses mongoose for single insert and read operation per execution.

I use the following code as a database util for my app

require '../objs/'
mongoose = require 'mongoose'

class DbManager
     constructor: (@dbname="daftar") ->     

    start_connection: ()-> mongoose.connect "mongodb://localhost/#{@dbname}"
    close_connection: ()-> mongoose.disconnect

    getDatabaseName       : ()-> return dbname
    getDatabaseConnection : ()-> return mongoose.connection

    saveNote : (noteObj)->      
        notes_model = mongoose.model "notes", DafNote.schema
        note = new notes_model noteObj
        note.save (err,dt)->
            if err
                console.log err

 # register to node root
 root.DbManager = DbManager

And this is the part calling that db util

require './src/core/objs/'
require './src/core/data/'

noteObj = new DafNote "test_title","this is a test body"
dbman = new DbManager
dbman.start_connection
dbman.saveNote noteObj
dbman.close_connection

And when I run the application it saves the object to the database but the application keeps waiting in terminal and don't close.

I've traced the problem, and I've thatmongoose.connect function is the reason causing my app not to close even if I've called mongoose.disconnect.

I really think that it may be the nature of the connect and disconnect function being asynchronous is causing that problem.

How can I solve this problem to make the application close after saving to mongodb and not to freeze at terminal?

Sameh K. Mohamed
  • 2,323
  • 4
  • 29
  • 55

1 Answers1

2

You really should be working off of the events and the callbacks. Mongoose exposes the connection events through the .connection accessor:

mongoose.connection.on("open",function(err,conn) {
  if (err) throw err;

  dbman.saveNote(function(err) {
     // Have your function return the callback and return any error

     // Then call disconnect
     mongoose.disconnect(); 
  });
});

mongoose.connection.on("close",function(err,conn) {
   // Which calls this as the connection is closed

   // In case you have something else running
   process.exit(); 
});

Encapsulate all of that in your class if you want. But you do want to implement callbacks on when tasks are complete.

Also not sure what else you might be running other than a mongoose connection, so if you have a command line program that needs to close call process.exit() as well to shut down the node event loop.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • I don't want to use `process.exit`, And the `saveNote` function won't be the only function so I think using the `on` events will limit code modularity. – Sameh K. Mohamed Jul 15 '14 at 15:15
  • And I want to know why the program waits If the connection if closed ? and if it's not closed, why it don't close even if I've called `mongoose.disconnect` – Sameh K. Mohamed Jul 15 '14 at 16:19
  • @SamehKamal The real point here was waiting for callbacks and events which your present code does not seem to do. Secondly, unless we can see **all** of your code, there may be other reasons why node does not hang up other than the disconnect being issued. Shell of code above that works for the rest of us who have been doing it for a while. – Neil Lunn Jul 15 '14 at 23:28
  • The problem was that the connection has never been closed, and I've solved the problem with setting timeout for the close connection function. – Sameh K. Mohamed Jul 16 '14 at 06:05