`waitForIndex` 不能正常工作
`waitForIndex` does not work correctly
当启动下面的脚本时,我遇到了一个卡在
上的错误
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'),
])
)
...
};
我认为一切看起来都不错,但您遗漏了查询的 .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'),
])
)
...
};
你好像混杂了不同Promise的APIAPI。我没有看到你从任何地方导入 Promise
。我猜你使用的是内置 JavaScript Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
当您使用 Promise.all
时,您必须将其与 then
链接起来,而不是使用 return
作为下一个语句。使用 resolver/reject.
链接到 then
您的代码在 Promise.all
之后使用 return
,但您没有调用 then
,因此它 returns 并且索引尚未准备好尚未创建。
此代码有效:
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
对象,而不是返回
当启动下面的脚本时,我遇到了一个卡在
上的错误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'),
])
)
...
};
我认为一切看起来都不错,但您遗漏了查询的 .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'),
])
)
...
};
你好像混杂了不同Promise的APIAPI。我没有看到你从任何地方导入 Promise
。我猜你使用的是内置 JavaScript Promise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
当您使用 Promise.all
时,您必须将其与 then
链接起来,而不是使用 return
作为下一个语句。使用 resolver/reject.
then
您的代码在 Promise.all
之后使用 return
,但您没有调用 then
,因此它 returns 并且索引尚未准备好尚未创建。
此代码有效:
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
对象,而不是返回