首页 > 解决方案 > hapi.js 没有从 mysql 返回值

问题描述

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

标签: javascriptmysqlhapijs

解决方案


推荐阅读