0

I am trying to get data that is at least three months fresh, using moment js but keep getting this error:

get userPassed error: 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 ' `_isUTC` = false, `_pf` = '[object Object]', `_locale` = '[object Object]', `_d' at line 1

Here is my code using nodejs with momentjs and mysql:

var today = new Date();
var threeMonthsAgo = moment.utc(today).subtract(3, 'months').format("YYYY-MM-DD HH:mm:ss").toISOString();

var qSelect = "SELECT id FROM Dislikes WHERE user_id = ? AND disliked_by_id = ? AND date_added > ?";
var qValues = [user_profile_id, user_pref_id, threeMonthsAgo];
var qCall = mysql.format(qSelect, qValues);
connection.query(qCall, function(err, userPassed, fields) {
    if (err) {
        console.log("get userPassed error: " + err);

Any ideas, I tried messing with the format of the date so far by adding utc and even the same setup for mysql

GMB
  • 216,147
  • 25
  • 84
  • 135
Leo
  • 23
  • 1
  • 5
  • Does this answer your question? [Select mysql query between date?](https://stackoverflow.com/questions/1469689/select-mysql-query-between-date) – ndogac Sep 29 '20 at 21:23

1 Answers1

2

It looks like your date variable is not properly passed to the query. I would recommend doing the date computation directly in the database, since this is likely to be more efficient:

var qSelect = "SELECT id FROM Dislikes WHERE user_id = ? AND disliked_by_id = ? AND date_added > current_date - interval 3 month";
var qValues = [user_profile_id, user_pref_id];
GMB
  • 216,147
  • 25
  • 84
  • 135