0

I am trying to implement interfacing for storing data to cassandra using helenus module. Using thrift driver, I am inserting and getting the data.

                keySpace.get(cfName, function (err, cf) {
                    cf.insert(key, storData, {consistency :
                              helenus.ConsistencyLevel.ANY}, function(err) {
               }

Now I am getting the data as below:

               keySpace.get(cfName, function (err, cf) {
                    cf.get(key, function(err, row) {
                       row.forEach(function(name,value,ts,ttl){
                       console.log("Getting NAME AS:", name.toString(),
                                   value.toString());
                    });
                });

But what I am observing is if the data stored is JSON level 1, then I am able to retrieve the data correctly, as for example:

                {"a":"b", "c":"d"}

This I am able to retrieve using above get operation, but if it is multi level, then I am getting the data as [Object Object], example JSON:

         {"c": [{"a":"b"}]}

This is not working for me. Please let me know where I am doing mistake.

Regards, -M-

u_peerless
  • 644
  • 2
  • 9
  • 23

1 Answers1

2

The string representation of an object is [Object Object], so when you have nested objects, that's what toString() will return.

You should be using JSON.stringify and JSON.parse instead, and if the name is never nested, you could do

keySpace.get(cfName, function (err, cf) {
     cf.get(key, function(err, row) {
         row.forEach(function(name,value,ts,ttl){
         console.log("Getting NAME AS:", name.toString(), JSON.stringify(value));
     });
});

basically the proper way to store JSON is to stringify it first, then when you're getting it back you parse it.

As a sidenote, why aren't you using CQL, it's a lot easier to work with compared to thrift

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • While storing the data, I am storing in JSON.strified format, but while getting the data, it is not coming properly in case of more than one level of JSON data, So I am thinking if consistencyLevel needs to set some thing different value or not. I will try with CQL parallelly. – u_peerless Jan 07 '14 at 07:05
  • As long you're storing a string, and you're using `JSON.stringify()` and not `toString()`, consistency levels or returning `[Object, Object]` shouldn't be an issue. – adeneo Jan 07 '14 at 08:09