2016-04-29 220 views
0

以下時啓動腳本中,我得到了被困在`waitForIndex`不能正常工作

Something bad heppened while waiting for index created 
Index `createdAt` was not found on table `olive.todos` 
r.table("todos").indexWait("createdAt") 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

但啓動腳本再次,我沒有問題的錯誤。

這是RethinkDB的問題還是我的? 並告訴我解決方案。

const createIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexList().contains(indexName) 
    .do(containsIndex => 
     r.branch(
     containsIndex, 
     { created: 0 }, 
     r.table(tableName).indexCreate(indexName) 
    ) 
    ) 
    ... 

const waitForIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexWait(indexName) 
    ... 

export const setup = startApp => { 
    r.connect(config.rethinkdb) 
    ... 
    .then(conn => { 
     Promise.all([ 
     createIndex(conn, 'todos', 'createdAt'), 
     createIndex(conn, 'days', 'date'), 
     ]); 
     return conn; 
    }) 
    .then(conn => 
     Promise.all([ 
     waitForIndex(conn, 'todos', 'createdAt'), 
     waitForIndex(conn, 'days', 'date'), 
     ]) 
    ) 
    ... 
}; 

回答

1

你似乎混合了不同的Promise API的API。我沒有看到你從任何地方進口Promise。我認爲你正在使用內置的JavaScript無極https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

當您使用Promise.all,你必須與then鏈它,而不是使用return爲下一條語句。連鎖到then與解析器/拒絕。

您的代碼使用return就在Promise.all之後,但您不要撥then,因此它立即返回並且索引尚未準備好創建。

此代碼:

import r from 'rethinkdb'; 
import config from '../config'; 

const createDatabase = (conn, name) => 
    r.dbList().contains(name) 
    .do(containsDb => 
     r.branch(
     containsDb, 
     { created: 0 }, 
     r.dbCreate(name) 
    ) 
    ) 
    .run(conn) 
    .error(err => { 
     console.log(`Could not create ${name} DB`); 
     console.log(err.message || 'Something bad happened'); 
     process.exit(1); 
    }); 

const createTable = (conn, name) => 
    r.tableList().contains(name) 
    .do(containsTable => 
     r.branch(
     containsTable, 
     { created: 0 }, 
     r.tableCreate(name) 
    ) 
    ) 
    .run(conn) 
    .error(err => { 
     console.log(`Could not create ${name} table`); 
     console.log(err.message || 'Somthing bad happened'); 
     process.exit(1); 
    }); 

const createIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexList().contains(indexName) 
    .do(containsIndex => 
     r.branch(
     containsIndex, 
     { created: 0 }, 
     r.table(tableName).indexCreate(indexName) 
    ) 
    ) 
    .run(conn) 
    .error(err => { 
     console.log(`Could not crate index ${indexName} in ${tableName}`); 
     console.log(err.message || 'Something bad happened'); 
     process.exit(1); 
    }); 

const waitForIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexWait(indexName) 
    .run(conn) 
    .error(err => { 
     console.log('Something bad happened while waiting for index created'); 
     console.log(err.message || 'Something bad happened'); 
     process.exit(1); 
    }); 

export const setup = startApp => { 
    r.connect(config.rethinkdb) 
    .then(conn => { 
     createDatabase(conn, 'test'); 
     return conn; 
    }) 
    .then(conn => { 
     return Promise.all([ 
     createTable(conn, 'todos'), 
     createTable(conn, 'days'), 
     ]).then((result) => conn, (reason) => conn) 
    }) 
    .then(conn => { 
     return Promise.all([ 
     createIndex(conn, 'todos', 'createdAt'), 
     createIndex(conn, 'days', 'date'), 
     ]).then(() => conn,() => conn) 
    }) 
    .then(conn => { 
     return Promise.all([ 
      waitForIndex(conn, 'todos', 'createdAt'), 
      waitForIndex(conn, 'days', 'date'), 
     ]) 
     } 
    ) 
    .then(() => { 
     console.log('DB and tables are available, starting Koa ...'); 
     startApp(); 
    }) 
    .error(err => { 
     console.log('Could not open a connection to initiailize the database'); 
     console.log(err.message || 'Something bad happened'); 
     process.exit(1); 
    }); 
}; 

setup() 

注意,我們必須使用then傳遞下去conn對象,而不是通過返回

+0

謝謝!現在,我明白了什麼是問題。爲了確保在調用'Promise.all'後調用'return conn',需要使用'then'鏈接。 – nishitani

+0

你能接受答案:)。謝謝 – kureikain

0

我認爲一切看起來不錯,但你錯過了.run(conn)部分的查詢。改變它應該這樣做:

const createIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexList().contains(indexName) 
    .do(containsIndex => 
     r.branch(
     containsIndex, 
     { created: 0 }, 
     r.table(tableName).indexCreate(indexName) 
    ) 
    ).run(conn); 
    ... 

const waitForIndex = (conn, tableName, indexName) => 
    r.table(tableName).indexWait(indexName).run(conn) 
    ... 

export const setup = startApp => { 
    r.connect(config.rethinkdb) 
    ... 
    .then(conn => { 
     Promise.all([ 
     createIndex(conn, 'todos', 'createdAt'), 
     createIndex(conn, 'days', 'date'), 
     ]); 
     return conn; 
    }) 
    .then(conn => 
     Promise.all([ 
     waitForIndex(conn, 'todos', 'createdAt'), 
     waitForIndex(conn, 'days', 'date'), 
     ]) 
    ) 
    ... 
}; 
+0

感謝您的回答,但'...'已經包含了'.RUN (conn)',並且應該有另一個問題。 (對不起,我的遺漏...我無法粘貼所有的代碼因爲它太長了。) – nishitani

+0

所有代碼可在這裏,[https://gist.github.com/nishitaniyuki/cb579a0aba7821028e49c0d483a377cf](https://gist .github.com/nishitaniyuki/cb579a0aba7821028e49c0d483a377cf) – nishitani