0

I am new to hapi and mysql and I am having trouble returning a value in the browser. It is working in the console.log but when going to localhost:3000/hello it is getting an error.

    server.route({
    method:'GET',
    path:'/hello',
    handler:function(request,h) {

        connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
            if (error) throw error;

            console.log('The solution is: ', results[0].solution);

            return ('The solution is: ', results[0].solution)
          });

    }
});

the console.log is working and is returning The solution is: 2 but when i go to http://localhost:3000/hello the error is Error: handler method did not return a value, a promise, or throw an error

I search for a solution and tried returning a value like this:

server.route({
    method:'GET',
    path:'/hello',
    handler:function(request,h) {

        connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
            if (error) throw error;

            console.log('The solution is: ', results[0].solution);

            return ('The solution is: ', results[0].solution)
          });
        // I returned 'hello world' outside the connection.query
          return "hello world"
    }
});

Now this is working except the input is hello world instead of The solution is: 2

How can I return return ('The solution is: ', results[0].solution) ?

Thank you in advance if it's helpful, here is my connection variable.

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'password',
  database : "dbname",
  insecureAuth : true
});
bradrar
  • 647
  • 1
  • 8
  • 19
  • 2
    Check [this post](https://stackoverflow.com/questions/14220321). If your MySQL library doesn't handle promises, you may wrap the call to `query` in a new `Promise` as shown in the accepted answer. – Stock Overflaw Apr 08 '19 at 08:26
  • Okay, I solved it. Thanks – bradrar Apr 08 '19 at 15:07

0 Answers0