mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
363 stars 93 forks source link

Parameter named undefined is not set #238

Closed Xaymar closed 1 year ago

Xaymar commented 1 year ago

I've been running into the error mentioned in the title with any query, prepared or not, that uses ? placeholders. I could not find any resources that explain what this error means, and the errno also did not match anything on the MariaDB error page. For sanity reasons I created a table that matches the example here:

CREATE TABLE `test`.`mytable`  (
  `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT,
  `someVal` int NULL,
  `otherVal` varchar(255) NULL,
  PRIMARY KEY (`id`)
);

And even the example results in this error. How can I fix this? It does not happen with the MySQL connectors, or other languages (tested in PHP and C++, worked fine).

rusher commented 1 year ago

you probably set option namedPlaceholders. In that case, queries must be like this :

const res = await conn.execute('SELECT * FROM mytable WHERE someVal = :someVal and otherVal = :otherVal', 
    { someVal: 1, otherVal: 'val1' }
);

in place of:

const res = await conn.execute('SELECT * FROM mytable WHERE someVal = ? and otherVal = ?', [1, 'val1']);

still, in this case, error message must be improved.

Xaymar commented 1 year ago

Thanks for the help, this actually helped me track down a bug in my own configuration file parser, which incorrectly set known-but-not-used keys to new Object(null) instead of undefined.

Just as an FYI, the wording on this page isn't clear about the use of named placeholders, as it doesn't say you can't use ? anymore.