`waitForIndex`无法正常工作

时间:2016-04-29 16:39:36

标签: rethinkdb rethinkdb-javascript

在下面启动脚本时,我遇到了错误

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'),
      ])
    )
    ...
};

2 个答案:

答案 0 :(得分:1)

您似乎混合了不同Promise API的API。我没有看到你从任何地方导入Promise。我猜你正在使用内置的JavaScript Promise 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个对象,而不是通过返回

答案 1 :(得分: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'),
      ])
    )
    ...
};