0

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

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

2 回答 2

1

你似乎混合了不同 Promise API 的 API。我没有看到你Promise从任何地方进口。我想你正在使用内置的 JavaScript Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

使用 时Promise.all,必须将其与 , 链接起来then,而不是使用returnas next 语句。then与解析器/拒绝链接。

您的代码return在之后使用 so ,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对象,而不是通过返回

于 2016-05-03T07:26:12.980 回答
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'),
      ])
    )
    ...
};
于 2016-05-02T15:46:38.223 回答