Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

dataserver17 avatar image
dataserver17 asked ·

Target data type could not be guessed, you should use prepared statements

Hello, why I get this error message?

TypeError: Target data type could not be guessed, you should use prepared statements for accurate type mapping. Value: {
    full_name: 'Jsjsjsjs',
    profile_image: 'NULL',
    phone: 'NU
}

User Type user_information:

CREATE TYPE user_information (
full_name text,
profile_image text,
phone text );

Query:

const query = `INSERT INTO users (user_id, email, password, user_details, user_since) VALUES (?, ?, ?, ?, ?);`;
const result = await client.execute(query, [ userid, body.email, body.password, 
 {
   full_name: body.fullname, 
   profile_image: 'NULL',
   phone: 'NULL
 }, 
 luxon.DateTime.now() ], { prepared: true });
user-defined typenode.js driver
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

absurdfarce avatar image
absurdfarce answered ·

After digging into this a bit it looks like your nodejs client isn't aware of the user_information UDT type; that's why the encoder doesn't know how to convert it to something that can be sent to Cassandra. You're using a prepared query here which does simplify things when dealing with UDTs since the query is prepared on the server and thus the driver can retrieve type information for the query from the server and have confidence that it's workable. But in this case that type information apparently isn't being retrieved from the server, and after some local testing I'm fairly certain this is because the flag you want to use for this case is "prepare" rather than "prepared". In my local testing I can reproduce the error cited above when using "prepared" but everything just worked after I fixed the typo.


For anyone coming across this issue after the fact https://docs.datastax.com/en/developer/nodejs-driver/4.6/features/datatypes/udts/ has some useful information on this point.


A minor additional nit: it looks like you're missing a closing single quote after the "NULL" value for user_information.phone in the sample query above.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.