NodeJS + Cassandra:无法检索布尔和整数数据

时间:2015-01-23 10:08:57

标签: node.js cassandra

我在带有cassandra数据存储区的nodejs上构建了一个webapp。

表架构看起来像这样:

'id uuid PRIMARY KEY,' +
'model varchar,' +
'price int,' +
'isAvailable boolean,' + 
'profit int,' + 
'lastSaleDate timestamp'

我能够成功地追踪记录。我正在使用预备语句来插入。

当我检索时,虽然我得到了记录,但我无法获得除uuid和varchar数据类型之外的任何值(即id& model)

console.log返回以下内容:

id: '5210d4ea-9f57-4159-aee4-026a256841ee',
model: 'carModel1',
price: undefined,
isAvailable: undefined,
profit: undefined,
lastSaleDate: undefined

我需要做什么才能正确检索剩余数据?

编辑(nodejs代码按要求):

创建表格

client.execute('CREATE TABLE IF NOT EXISTS franchise.cars (' +
                'id uuid PRIMARY KEY,' +
                'model varchar,' +
                'price int,' +
                'isAvailable boolean,' + 'profit int,' + 'lastSaleDate timestamp' +
                ');',
                next);

添加记录

var upsertCars = 'INSERT INTO franchise.cars (id, model, price, isAvailable, profit, lastSaleDate)  '
    + 'VALUES(?, ?, ?, ?, ?, ?);';

var id = null;
if ( ! req.body.hasOwnProperty('id')) {
    id = cassandra.types.uuid();
} else {
    id = req.body.id;
}

var date_as_timestamp=new Date(req.body.lastSaleDate).getTime();

client.execute(upsertCars,
        [id, req.body.model, req.body.price, req.body.isAvailable, req.body.profit, date_as_timestamp],{prepare:true},
        afterExecution('Error: ', 'Car ' + req.body.model + ' upserted.', res));

获取记录:

var getCars = 'SELECT *FROM franchise.cars';

var cars=[];

    client.execute(getCars,{prepare:true}, function(err, result) {
        if (err) {
            res.status(404).send({ msg : 'Car not found.' });
        } else {
for(var i=0; i<result.rows.length;i++){
console.log(result.rows[i].isAvailable);
machines.push({id : result.rows[i].id, model : result.rows[i].model, price : result.rows[i].price, isAvailable : result.rows[i].isAvailable, profit : result.rows[i].profit, lastSaleDate : result.rows[i]. lastSaleDate});
}

1 个答案:

答案 0 :(得分:1)

虽然插入需要附带正确的属性,但是获取数据应该使用小写的属性名称

var upsertCars = 'INSERT INTO franchise.cars (id, model, price, isAvailable, profit, lastSaleDate)  '
    + 'VALUES(?, ?, ?, ?, ?, ?);';

适用于插入,但在获取时:

var getCars = 'SELECT *FROM franchise.cars';

var cars=[];

    client.execute(getCars,{prepare:true}, function(err, result) {
        if (err) {
            res.status(404).send({ msg : 'Car not found.' });
        } else {
for(var i=0; i<result.rows.length;i++){
console.log(result.rows[i].isAvailable);
machines.push({id : result.rows[i].id, model : result.rows[i].model, price : result.rows[i].price, isAvailable : result.rows[i].isAvailable, profit : result.rows[i].profit, lastSaleDate : result.rows[i]. lastSaleDate});
}

需要替换为:

var getCars = 'SELECT *FROM franchise.cars';

var cars=[];

    client.execute(getCars,{prepare:true}, function(err, result) {
        if (err) {
            res.status(404).send({ msg : 'Car not found.' });
        } else {
for(var i=0; i<result.rows.length;i++){
console.log(result.rows[i].**isavailable**);
machines.push({id : result.rows[i].id, model : result.rows[i].model, price : result.rows[i].price, isAvailable : result.rows[i].**isavailable**, profit : result.rows[i].profit, lastSaleDate : result.rows[i]. **lastsaledate**});
}