javascript - 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
});
解决方案
推荐阅读
- reactjs - 如何使用共享组件组织两个 React 项目?
- python - 将 Pandas DF 导出为嵌套 JSON(多重嵌套)
- go - Golang crypto/rsa 问题使用 PEM 文件中的私钥解密文本
- node.js - Passport session - 我想在 session 中存储用户信息,但在浏览器中只存储 id
- python - Visual Studio:未解决的导入“numpy”
- python - 在 instagram api 登录(instagrapi)上无法通过 429 错误(请求太多)
- apache - htaccess 标头未应用于 http 文件
- neo4j - Kafka Neo4j Connect - 使用螺栓+路由协议
- c# - 将文件复制到远程并创建目录和递归复制
- date - 如何使批处理文件在某个日期执行与正常情况不同的事情?