2017-05-14 6 views
1

私はこれまでにここで育てられたトピックだと知っていますが、私はrethinkdbでasync.waterfallを使用しています。私はError: Callback was already calledを得ています。奇妙な部分は、それがそのエラーをスローし、アプリケーションをクラッシュさせるにもかかわらず、私は必要なデータベースとテーブルを作成することです。私はNodeJS Async: Callback already called?またはUsing async.waterfallのような回答をいくつか他の投稿を読んだが、どこにも出てこないようだ。私のコンソールでは、エラーはdb.js:40:9にあるとも言われていますが、私はNodeを初めて知っていて、コールバックで何を望んでいるのか分かりません。私は間違って何をしていますか?コールバックをここにネストする必要がありますか?私が使用しているコードは以下の通りです。私がここに得ることができるどんな助けも大いに評価され、必要ならば私は他の関連するコードを掲載することができます。みんなありがとう。Async.waterfall:コールバックは既に呼び出されましたか?

db.js:

exports.setDatabaseAndTables = function() { 
     async.waterfall([ 

     function connect(callback) { 
      r.connect(config.rethinkdb, callback); 
     }, 

     function createDatabase(connection, callback) { 
      // Create the database if needed. 
      r.dbList().contains(config.rethinkdb.db).do(function(containsDb) { 
      return r.branch(
       containsDb, 
       {created: 0}, 
       r.dbCreate(config.rethinkdb.db) 
      ); 
      }).run(connection, function(err) { 
      callback(err, connection); 
      }); 
     }, 

     function createTable(connection, callback) { 
      // Create the tables if needed. 
      r.tableList().contains('todos').do(function(containsTable) { 
      return r.branch(
       containsTable, 
       {created: 0}, 
       r.tableCreate('todos') 
      ); 
      }).run(connection, function(err) { 
      callback(err, connection); 
      }); 
      r.tableList().contains('users').do(function(containsTable) { 
      return r.branch(
       containsTable, 
       {created: 0}, 
       r.tableCreate('users') 
      ); 
      }).run(connection, function(err) { 
      callback(err, connection); 
      }); 
     }, 

     function createIndex(connection, callback) { 
      // Create the indexes if needed. 
      r.table('todos').indexList().contains('createdAt').do(function(hasIndex) { 
      return r.branch(
       hasIndex, 
       {created: 0}, 
       r.table('todos').indexCreate('createdAt') 
      ); 
      }).run(connection, function(err) { 
      callback(err, connection); 
      }); 
      r.table('users').indexList().contains('createdAt').do(function(hasIndex) { 
      return r.branch(
       hasIndex, 
       {created: 0}, 
       r.table('users').indexCreate('createdAt') 
      ); 
      }).run(connection, function(err) { 
      callback(err, connection); 
      }); 
     }, 

     function waitForIndex(connection, callback) { 
      // Wait for the index to be ready. 
      r.table('todos').indexWait('createdAt').run(connection, function(err, result) { 
      callback(err, connection); 
      }); 
      r.table('users').indexWait('createdAt').run(connection, function(err, result) { 
      callback(err, connection); 
      }); 
     } 
     ], 

     function(err, connection) { 
     if(err) { 
      console.error(err); 
      process.exit(1); 
      return; 
     } 
     }); 
    }; 
+2

'createTable'、' waitForIndex'各々は '(2回callback'呼び出しcreateIndex'と':ここ

は2つの機能毎に "のcreateTable"、 "CREATEINDEX" および "waitForIndex" をsplitingによって解決しますたとえば、後者は 'createdAt'の' todos'インデックスが作成されたときに 'callback'を呼び出しますが、' users'のインデックスが作成されたときにも呼び出すので)、別々のステップに分割する必要があります。 "waterfall step"では、コールバックは一度しかコールできません。 – robertklep

+1

@robertklepありがとうRobert!あなたはまさに正しいのです。私はいくつかのコードを再構成する必要はなくなりましたが、それを有効にすることができました。 –

答えて

1

これは "async.waterfall" を試したもののために共通の問題です。

exports.setDatabaseAndTables = function() { 
    async.waterfall([ 

     function connect(callback) { 
      r.connect(config.rethinkdb, callback); 
     }, 

     function createDatabase(connection, callback) { 
      // Create the database if needed. 
      r.dbList().contains(config.rethinkdb.db).do(function(containsDb) { 
       return r.branch(
         containsDb, 
         {created: 0}, 
         r.dbCreate(config.rethinkdb.db) 
         ); 
      }).run(connection, function(err) { 
       callback(err, connection); 
      }); 
     }, 

     function createTableTodos(connection, callback) { 
      // Create the tables if needed. 
      r.tableList().contains('todos').do(function(containsTable) { 
       return r.branch(
         containsTable, 
         {created: 0}, 
         r.tableCreate('todos') 
         ); 
      }).run(connection, function(err) { 
       callback(err, connection); 
      }); 
     }, 

     function createTableUsers(connection, callback) { 
      r.tableList().contains('users').do(function(containsTable) { 
       return r.branch(
         containsTable, 
         {created: 0}, 
         r.tableCreate('users') 
         ); 
      }).run(connection, function(err) { 
       callback(err, connection); 
      }); 
     }, 

     function createIndexTodos(connection, callback) { 
      // Create the indexes if needed. 
      r.table('todos').indexList().contains('createdAt').do(function(hasIndex) { 
       return r.branch(
         hasIndex, 
         {created: 0}, 
         r.table('todos').indexCreate('createdAt') 
         ); 
      }).run(connection, function(err) { 
       callback(err, connection); 
      }); 
     }, 

     function createIndexUsers(connection, callback) { 
      r.table('users').indexList().contains('createdAt').do(function(hasIndex) { 
       return r.branch(
         hasIndex, 
         {created: 0}, 
         r.table('users').indexCreate('createdAt') 
         ); 
      }).run(connection, function(err) { 
       callback(err, connection); 
      }); 
     }, 

     function waitForIndexTodos(connection, callback) { 
      // Wait for the index to be ready. 
      r.table('todos').indexWait('createdAt').run(connection, function(err, result) { 
       callback(err, connection); 
      }); 
     }, 

     function waitForIndexUsers(connection, callback) { 
      r.table('users').indexWait('createdAt').run(connection, function(err, result) { 
       callback(err, connection); 
      }); 
     } 
    ], 
      function(err, connection) { 
       if(err) { 
        console.error(err); 
        process.exit(1); 
        return; 
       } 
      }); 
}; 
関連する問題