How do I post data from req.body into a CQL UDT column using the Node.js driver?

92 views Asked by At

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

1

There are 1 answers

0
Erick Ramirez On

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!