This is nodejs program. How do I return the result?
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;
Instead of having an inline callback in the execute function (where you check your rows error) you could pass the callback in as Tamil suggested, like this
connection_pool.execute(query, [], callback);
Then you could pass the callback into your exported function
executeCQLQuery = function(query, PooledConnection, options, callback) {
...
}
Keep in mind that the cassandra node library is asynchronous, like most things in node.js. You need to do things the node way, with a callback function:
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;
then use it like this:
executeCQLQuery('select foo from bar where key="bam"', PooledConnection, {host:'foo', port:1234}, function(err, rows) {
// check for err
// do something useful with rows.
});
Another suggestion: It doesn't make sense to use PooledConnection as an arg. You should instantiate it before calling executeCQLQuery.
The test cases in cassandra-node are always a good place to start: http://code.google.com/a/apache-extras.org/p/cassandra-node/source/browse/test/test_driver.js#280
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.