hapi.js没有从mysql返回值

时间:2019-04-05 05:12:42

标签: javascript mysql hapijs

我是不熟悉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
});

0 个答案:

没有答案