0

I am trying to select a range of records based on a timestamp using NodeJS and node-mysql. My code looks like:

sql = "SELECT pressure, temp1, temp2, UNIX_TIMESTAMP(timestamp)*1000 
AS unix_timestamp
FROM observations WHERE timestamp >= STR_TO_DATE( ? ,'%Y-%m-%d') 
ORDER BY unix_timestamp DESC LIMIT ?;"

var current_temp = db.query(sql,
start_date, num_records,
  function(err, rows, fields){
     if (err){

The variable start_date contains 2013-03-01. If I manually replace "?" in the STR_TO_DATE expression with '2013-03-01' my query works. However, if I try to us variable replacement I get the following error:

TypeError: Object 500 has no method 'apply'
at Query.Sequence.end     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Query.ErrorPacket     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/sequences/Query.js:89:8)
at Protocol._parsePacket     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Protocol.js:169:24)
at Parser.write     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write     (/home/ian/Documents/emacs/org/org_files/computer/nodejs/node_modules/mysql/lib/protocol/Protocol.js:36:16)
at write (_stream_readable.js:547:24)
at flow (_stream_readable.js:556:7)
at Socket.pipeOnReadable (_stream_readable.js:588:5)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)

1 Answers1

0

Your variables should be in an array, positioned according to the order they appear in the SQL statement, like this:

    db.query(sql,[start_date, num_records],  function(err, rows, fields){ logic ;})
ExxKA
  • 930
  • 6
  • 10