2

In the below insert query when I use dateof(now()) it is not working. But new Date('2017-09-01 00:00:00+0000'), this one is working.

router.post('/recent_activities', function(req, res, next) {
  var body = req.body;

  console.log(body);
  console.log(body.id + body.setid);

  var query = "Insert into recent_activities 
    (id, setid, companyid, activity_name, activity_date, activity_by) values( ? , ? , ? , ? , ? , ? )
  "

  client.execute(query, [body.id, body.setid, body.companyid, body.activity_name, dateof(now()), body.activity_by],
    function(err, result) {
      if (err) {
        throw err;
      } else {
        console.log('success!');
      }
    });

  res.json({ id: 201, message: 'All Success' });
});

I am getting a 500 internal server error.

now is not defined

ReferenceError: now is not defined

at /apps/node_cassandra/routes/index.js:96:85

at Layer.handle [as handle_request] 

(/apps/node_cassandra/node_modules/express/lib/router/layer.js:95:5)
    at next 

(/apps/node_cassandra/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch 

(/apps/node_cassandra/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] 

(/apps/node_cassandra/node_modules/express/lib/router/layer.js:95:5)


    at /apps/node_cassandra/node_modules/express/lib/router/index.js:281:22
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
user3214361
  • 186
  • 1
  • 3
  • 13

3 Answers3

1

dateOf()

Used in a SELECT clause, this function extracts the timestamp of a timeuuid column in a result set. This function returns the extracted timestamp as a date. Use unixTimestampOf() to get a raw timestamp.

According to documentation dateOf is used in select clause.

now() is used to generate timeuuid for inserts.

So if your column datatype is timeuuid then only you can use now(). If it is timestamp insert the date as new Date().

Alternatively there is toDate function available, which can be used in insert

dateOf

undefined_variable
  • 6,180
  • 2
  • 22
  • 37
1

The problem...

client.execute(query,
[body.id,body.setid,body.companyid,body.activity_name, dateof(now()),body.activity_by],

...is that your code is referencing dateof(now()) as if it is a variable local to your Node.js code. I'd suggest putting it in quotes...

body.activity_name, "dateof(now())",body.activity_by],

...but that may not work the way you intend when it is treated as a parameter to your statement.

For this case, it may just be easier to modify your INSERT statement...

var query ="Insert into recent_activities 
    (id,setid,companyid,activity_name,activity_date,activity_by)
    values (?, ?, ?, ?, dateof(now()) , ?)"

...and subsequently remove it from your parameter list.

Note that dateOf(now()) is meant to be used with SELECT queries. Using it with an INSERT/UPDATE has a side-effect of storing milliseconds with your time value, but not actually showing you that they are there. See this for more info: Cassandra cqlsh - how to show microseconds/milliseconds for timestamp columns?

Community
  • 1
  • 1
Aaron
  • 55,518
  • 11
  • 116
  • 132
0
var query ="Insert into recent_activities (id,setid,companyid,activity_name,activity_date,activity_by) values (? , ?, ?, ?, **dateof(now())**, ?)"
    client.execute(query,[body.id,body.setid,body.companyid,body.activity_name,body.activity_by], function(err, result) {

adding "dateof(now())" worked for me by adding in the insert values.

user3214361
  • 186
  • 1
  • 3
  • 13