   getInstance: (table, after) ->
     throw new Error 'The database name must be configured (options.db)' unless @options.db?
 
-    if @db isnt null and @db.state is 'connected'
+    if @db isnt null and @db.state and @db.state is 'connected'
       @getCollection table, after
     else
       instance = new Db @options.db, new Server @options.host, @options.port, {}

        after collection

  findById: (table, id, after = ->) ->
    @getInstance table, (collection) =>
      try
        params = _id: ensureObjectId id
      catch exception
        console.log 'Error with preparing params for findById: ' + exception

        @close()
        return after false

      collection.find(params).toArray (error, results) =>
        if error
          console.log 'Error with fetching document by id: ' + error

        after parseInt results, 10

  save: (table, params, after = ->) ->
    try
      params._id = ensureObjectId params._id if params._id?
    catch exception
      console.log 'Error with preparing params for save: ' + exception

      @close()
      return after false

    @getInstance table, (collection) =>
      collection.save params, safe: true, (error, results) =>
        if error
          console.log 'Error with saving data: ' + error
