0

I am new to cassandra I need your help.

After creating a collection table using cql console, I am able to create new records and read them, but Post operation using cassandra-driver in nodejs is not working, it only works when I use cql console.

I created table:

CREATE TYPE event_info (
    type text,
    pagePath text,
    ts text,
    actionName text
);
CREATE TABLE journey_info_5 (
    id uuid PRIMARY KEY,
    user_id text,
    session_start_ts timestamp,
    event FROZEN<event_info>
);

codes for post operation:

export const pushEvent = async(req,res)=>{
    const pushEventQuery = 'INSERT INTO user_journey.userjourney (id, user_id, session_start_ts,events)
    VALUES ( ${types.TimeUuid.now()}, ${req.body.user_id},${types.TimeUuid.now()},
     { ${req.body.type},${req.body.pagePath},${req.body.ts},${req.body.actionName}} } );'

    try {
        
        await client.execute(pushEventQuery)
       res.status(201).json("new record added successfully");

    } catch (error) {
        res.status(404).send({ message: error });
        console.log(error);
    }
}

it is giving errors, How can I get data from user and post in this collection? please help me, if any idea

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you please share the error you are getting ? – Naveen Kulkarni Jun 11 '22 at 14:38
  • sure, this is the error that I am getting: no viable alternative at input '(' (... (id, user_id, session_start_ts,[(]...) info: 'Represents an error message from the server', – KOMEZUDUFASHE Arsene Thierry Jun 11 '22 at 16:15
  • more error infor: query: 'INSERT INTO user_journey.userjourney (id, user_id, session_start_ts,(events))\n' + ' VALUES ( 3fde4320-e9a1-11ec-8e88-6dfa37e4a54d, new user_id,3fde6a30-e9a1-11ec-a324-a8d27bd926ef, { undefined,undefined,undefined,undefined} } );' } – KOMEZUDUFASHE Arsene Thierry Jun 11 '22 at 16:15
  • From the error looks like your `req.body` is `undefined`. Are you using body parser ? Can you see if you are getting anything in `req.body` – Naveen Kulkarni Jun 12 '22 at 17:18

1 Answers1

0

The issue is that your CQL statement is invalid. The format for inserting values in a user-defined type (UDT) column is:

    { fieldname1: 'value1', fieldname2: 'value2', ... }

Note that the column names in your schema don't match up with the CQL statement in your code so I'm reposting the schema here for clarity:

CREATE TYPE community.event_info (
    type text,
    pagepath text,
    ts text,
    actionname text
)
CREATE TABLE community.journey_info_5 (
    id uuid PRIMARY KEY,
    event frozen<event_info>,
    session_start_ts timestamp,
    user_id text
)

Here's the CQL statement I used to insert a UDT into the table (formatted for readability):

INSERT INTO journey_info_5 (id, user_id, session_start_ts, event)
  VALUES (
    now(),
    'thierry',
    totimestamp(now()),
    {
      type: 'type1',
      pagePath: 'pagePath1',
      ts: 'ts1',
      actionName: 'actionName1'
    }
  );

For reference, see Inserting or updating data into a UDT column. Cheers!

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23