Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js cassandra connecion_Node.js_Cassandra - Fatal编程技术网

node.js cassandra connecion

node.js cassandra connecion,node.js,cassandra,Node.js,Cassandra,这是nodejs程序。如何返回结果 executeCQLQuery = function(query, PooledConnection, options) { var hosts = [options.host + ":" + options.port]; var connection_pool = new PooledConnection({ 'hosts': hosts, 'keyspace': options.keyspace }); connectio

这是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;

不必在execute函数中使用内联回调(在这里检查行错误),您可以按照泰米尔的建议传入回调,如下所示

connection_pool.execute(query, [], callback);
然后可以将回调传递到导出的函数中

executeCQLQuery = function(query, PooledConnection, options, callback) {
  ...
}

请记住,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节点中的测试用例总是一个很好的起点:

您使用的是Apache cassandra节点吗?是的,我需要从另一个文件返回值。查看我的示例代码。使用模块使用回调函数,该函数将接受resultset作为参数函数(err,rows,cb){//所有逻辑;cb(result);}