Node.js Api, can't delete from database

Viewed 552

I'm working on an Angular project so I wanted to make a simple Rest API to handle a MySql database, that's why I made one in Node.js but when I tried to delete an item with id = 3, I'm only getting an error that looks like this:

  errno: 1064,
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1",
  sqlState: '42000',
  index: 0,
  sql: 'DELETE FROM recipies WHERE id = ?'

So I wanted to ask for help. I was thinking that I'm getting this error because these few lines had some logic problems:

Recipe.remove = (id, result) => {
    sql.query("DELETE FROM recipies WHERE id = ?", id, (err,res) => {
        if (err) {
            console.log("error", err);
            result(null, err);
            return;
        }
        if (res.affectedRows == 0) {
            // not found recipe with the id
            result({ kind: "not_found" }, null);
            return;
        }
        console.log("deleted customer with id: ", id);
        result(null, res);
    });
};
2 Answers

The error message was telling you all along!

In NodeJS, to replace query arguments you list them in an array as the second argument of the query.

sql.query("DELETE FROM recipies WHERE id = ?", [ id ], (err,res) => {...

A typo appeared: recipeID, should be recipeId

But mods edited my post so you will probably never know where...

Remember, always see if you have typos

Related