warwcat warwcat - 1 year ago 126
Node.js Question

Node.js stops on options

I send an XMLHttpRequest of type POST and when I do it the server shutdown and says

this._callback.apply(this, arguments);

TypeError: this._callback.apply is not a function
at Query.Sequence.end (/usr/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at /usr/lib/node_modules/mysql/lib/protocol/Protocol.js:399:18
at Array.forEach (native)
at /usr/lib/node_modules/mysql/lib/protocol/Protocol.js:398:13
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

My server code:

var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var mysql = require('mysql');

var app = express();

app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({extended: true}));

var db_con = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '****',
database : 'quizz'

app.post('/questions', function (request, response){
var query = db_con.query("SELECT DISTINCT question_id, description FROM QUESTION WHERE country=?", [request.body.country]," ORDER BY RAND() LIMIT 5", function(err,result ) {
if (err)

response.json({"questions": result});

var server = app.listen(8080, function() {
console.log('Listening at http://%s:%s', server.address().address, server.address().port);

client code:

function get_questions(){
var xhr = new XMLHttpRequest();
xhr.open('POST' , 'http://localhost:8080/questions', true);

xhr.onload = function(){

if (!response.error) {
var response = JSON.parse(xhr.responseText);
var Score = response;
var output = Score;


xhr.send(JSON.stringify( {"country": "Saudi Arabia"} ) );

Answer Source

Your query should look like that:

var query = db_con.query("SELECT DISTINCT question_id, description FROM QUESTION WHERE country=? ORDER BY RAND() LIMIT 5", [request.body.country], function(err,result) {
  if (err)

  response.json({"questions": result});

The first argument should be the query string, the second should be parameters, and the third should be callback.

The next time you have similar problem, RTFM first.