Node.js MySQL query error ER_PARSE_ERROR
<p>I have a problem with a MySQL query inside of a NodeJS application. In my snippet I loop through a triple-nested for cycle and save some different data in the <strong>post</strong> variable. What I want to do is checking whether this data already exists into the <strong>database</strong> and, if not, execute an <strong>INSERT</strong> query.</p>

<p>Been trying in any possible way, however I get the following error:</p>

<pre><code>Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `user_id` = '421', `product_id` = '1', `brick_id` = '1', `effect_date` = '2016-' at line 1
</code></pre>

<p>I don't think there are any SQL errors, I have been checking a thousand of times but maybe I am missing something. This is what I tried to do:</p>

<pre><code>router.post('/relations/finalize', function (req, res, next) {
    // Executing a cycle for every possible data combination
    for (var i = 0; i &lt; req.body.users_selection.length; i++) {
        for (var j = 0; j &lt; req.body.products_selection.length; j++) {
            for (var k = 0; k &lt; req.body.bricks_selection.length; k++) {
                post = {
                    owner_id: req.session.userid,
                    user_id: req.body.users_selection[i],
                    product_id: req.body.products_selection[j],
                    brick_id: req.body.bricks_selection[k],
                    effect_date: req.body.year + "-" + req.body.month + "-01 00:00:00",
                }

                // Checking if the entry already exists
                req.app.locals.connection.query({
                    sql: 'SELECT * FROM relations WHERE user_id = ? AND product_id = ? AND brick_id = ? AND effect_date &gt;= ?',
                    timeout: 40000, // 40s
                    values: [post]
                }, function (err, rows, fields) {
                    if (err) throw err;
                    else {
                        if (rows.length &gt; 0) {
                            console.log("The entry already esists");
                        } else {
                            req.app.locals.connection.query("INSERT INTO relations SET ?", post, function (err, row, fields) {
                                if (err) throw err;
                                else {
                                    console.log("The entry didn't exists and was then created.");
                                    //res.redirect("/relations/wizard");
                                }
                            });
                        }
                    }
                });
            }
        }
    }
});
</code></pre>

<p>Any help will be greatly appreciated to solve this issue.</p>

<p>EDIT: I also tried to pass separate values into the SELECT query, like this:</p>

<pre><code>values: req.body.users_selection[i], req.body.products_selection[j], req.body.bricks_selection[k], req.body.year + "-" + req.body.month + "-01 00:00:00"
</code></pre>

<p>But the result I get is that the INSERT query is called X times with the same values, as the for loops are not going through, maybe because of the asynchronous nature of Node..</p>