hapi.js не возвращает значение из MySQL - PullRequest
0 голосов
/ 05 апреля 2019

Я новичок в hapi и mysql, и у меня возникают проблемы с возвратом значения в браузере.Он работает в console.log, но при переходе на localhost:3000/hello он получает ошибку.

    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)
          });

    }
});

console.log работает и возвращает The solution is: 2, но когда я перехожу к http://localhost:3000/helloошибка: Error: handler method did not return a value, a promise, or throw 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)
          });
        // I returned 'hello world' outside the connection.query
          return "hello world"
    }
});

Теперь это работает, за исключением того, что ввод hello world вместо The solution is: 2

Как я могу вернуть return ('The solution is: ', results[0].solution)?

Заранее спасибо, если это полезно, вот моя переменная соединения.

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'password',
  database : "dbname",
  insecureAuth : true
});
...