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