这是nodejs程序。如何返回结果?
executeCQLQuery = function(query, PooledConnection, options) {
var hosts = [options.host + ":" + options.port];
var connection_pool = new PooledConnection({
'hosts': hosts,
'keyspace': options.keyspace
});
connection_pool.execute(query, [], function (err, rows) {
if (err) {
console.log(err);
} else {
return rows;
}
//console.log(rows);
result = rows;
});
console.log(result);
return result;
}
exports.executeCQLQuery = executeCQLQuery;
答案 0 :(得分:0)
而不是在执行函数中有一个内联回调(你检查你的行错误),你可以像泰米尔建议的那样传递回调,就像这样
connection_pool.execute(query, [], callback);
然后你可以将回调传递给导出的函数
executeCQLQuery = function(query, PooledConnection, options, callback) {
...
}
答案 1 :(得分:0)
请记住,cassandra节点库是异步的,就像node.js中的大多数东西一样。您需要使用回调函数以节点方式执行操作:
executeCQLQuery = function(query, PooledConnection, options, callback) {
var hosts = [options.host + ":" + options.port];
var connection_pool = new PooledConnection({
'hosts': hosts,
'keyspace': options.keyspace
});
connection_pool.execute(query, [], callback);
exports.executeCQLQuery = executeCQLQuery;
然后像这样使用它:
executeCQLQuery('select foo from bar where key="bam"', PooledConnection, {host:'foo', port:1234}, function(err, rows) {
// check for err
// do something useful with rows.
});
另一个建议:将PooledConnection用作arg是没有意义的。您应该在调用executeCQLQuery之前实例化它。
cassandra-node中的测试用例始终是一个很好的起点:http://code.google.com/a/apache-extras.org/p/cassandra-node/source/browse/test/test_driver.js#280