Abhi
Abhi

Reputation: 98

NodeJS + Cassandra: Unable to retrieve boolean and integer data

I have a webapp built on nodejs with cassandra datastore.

The table schema looks something like this:

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

I am able to succesfully upsert a record. I am using prepared statements to insert.

When I am retrieving, though I get the records, I am unable to get value for anything other than uuid and varchar datatype (i.e. id & model)

console.log returns the following:

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

What do I need to do to retrieve the remaining data correctly?

EDIT (nodejs code as requested):

Create Table:

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

Add record:

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));

Get Record:

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});
}

Upvotes: 0

Views: 276

Answers (1)

Abhi
Abhi

Reputation: 98

While the inserts needs to be accompanied with properly cased attributes, fetching the data should be with lowercased attribute name

ie.

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

is fine for insert, but while fetching:

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});
}

needs to be replaced with:

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**});
}

Upvotes: 1

Related Questions