   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 parseInt results, 10

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

        @close()
        return after false

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

  find: (table, params, options, after) ->
    [params, options, after] = @_normalizeArguments params, options, after

    try
      if params?._id?
        if isObject(params._id) and params._id.$in?
          params._id.$in = params._id.$in.map (value) -> ensureObjectId value
        else
          params._id = ensureObjectId params._id
    catch exception
      console.log 'Error with preparing params for find: ' + exception

      @close()
      return after []

    @getInstance table, (collection) =>
      cursor = collection.find params

      cursor.sort  options.sort  if options.sort
