Cassandra-client不会INSERT

时间:2012-09-14 18:10:35

标签: node.js cassandra

我不确定为什么这不起作用。我的表很简单(我甚至​​暂时将所有内容都更改为varchar),如果使用cqlsh,我可以插入。

    var Connection = require('cassandra-client').Connection;
    var db = new Connection({host: 'cassbx01.qualcomm.com', port: 9160, keyspace: 'foursq'});

    db.execute("INSERT INTO checkins (fqid, name, address, city, state, zip, country, lat, lng, hereNow) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
[value.id, value.name, value.location.address, value.location.city,
value.location.state, value.location.postalCode, value.location.country,
value.location.lat, value.location.lng, value.hereNow.count], 
function (err) {
   if (err || !saved) {
     throw err;
   }
});

运行时出现以下错误:

    self.client.execute_cql_query(cql, ttypes.Compression.NONE, function(err, 
                ^
TypeError: Cannot call method 'execute_cql_query' of null
    at Connection.execute (/usr2/lsacco/nodejs/foursq-poc/node_modules/cassandra-client/lib/driver.js:407:17)
    at exports.poller (/usr2/lsacco/nodejs/foursq-poc/controller/api.js:80:29)
    at Array.forEach (native)
    at Function._.each._.forEach (/usr2/lsacco/nodejs/foursq-poc/node_modules/underscore/underscore.js:76:11)
    at IncomingMessage.exports.poller (/usr2/lsacco/nodejs/foursq-poc/controller/api.js:77:19)
    at IncomingMessage.EventEmitter.emit (events.js:115:20)
    at IncomingMessage._emitEnd (http.js:366:10)
    at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
    at CleartextStream.socketOnData [as ondata] (http.js:1366:20)
    at CleartextStream.CryptoStream._push (tls.js:495:27)

1 个答案:

答案 0 :(得分:2)

您需要先启动与connect函数的连接,然后才能在回调中执行CQL。

一个简单的例子如下:

var Connection = require('cassandra-client').Connection;
var db = new Connection({host: '127.0.0.1', port: 9160, keyspace: 'foursq'});
db.connect(function(err) {
  if (err) {
    throw err;
  } else {
    db.execute('INSERT INTO people (key, name) VALUES (?, ?)', ['key1', 'paul'],
      function (err) {
        if (err) {
          throw err;
        } else {
          console.log('success!');
        }
    });
  }
});