0
votes

what am i doing wrong?

const mariadb = require("mariadb");

(() => {
  setTimeout(async () => {
    console.log("connecting to db...");
    try {
      const pool = await mariadb.createPool({
        host: "localhost", //172.17.0.1
        user: "root",
        database: "gamifydb",
        port: "3306",
        password: "password"
      });
      const conn = await pool.getConnection();
      global.conn = conn;
      console.log("mariadb db connected");
    } catch (error) {
      throw error;
    }
  }, 1000);
})();

const achievement = {
    name: "lorem",
    user_id: "lorem",
    challenge_id:2,
    desc:"lorem"
}

exports.createAchievement = async achievement => {
  try {
    const response = await global.conn.query(
      "INSERT INTO achievements value (?,?,?,?);",
      Object.values(achievement)
    );
    console.log(response);
    return response;
  } catch (error) {
    throw error;
  }
};

"(conn=1194, no: 1136, SQLState: 21S01) Column count doesn't match value count at row 1\nsql: INSERT INTO achievements value (?,?,?,?); - parameters:['lorem','lorem',2,'lorem']" }

1
This is a question where reading the error message carefully can lead you directly to the answer. - O. Jones

1 Answers

0
votes

Looks like your table achievements has more than 4, or less than 4, columns.

Name the columns in your INSERT statement. Something like this might work

INSERT INTO achievements (name, user_id, challenge_id, desc)
VALUES (?,?,?,?);

Oh, and it's VALUES, not value.