I am trying to connect hapi.js with mysql. But when defining a server.route. the handler
is not returning a value.
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)
});
}
});
It is saying Error: handler method did not return a value, a promise, or throw an error
.
In here, I am returning ('The solution is: ', results[0].solution)
But it is still not working.
The output in the console is The solution is: 2
but in the browser, It is an error.
Please help. Thank you
I am trying to connect hapi.js with mysql. But when defining a server.route. the handler
is not returning a value.
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)
});
}
});
It is saying Error: handler method did not return a value, a promise, or throw an error
.
In here, I am returning ('The solution is: ', results[0].solution)
But it is still not working.
The output in the console is The solution is: 2
but in the browser, It is an error.
Please help. Thank you
Share Improve this question asked Apr 5, 2019 at 9:52 bradrarbradrar 7371 gold badge9 silver badges21 bronze badges 4- 1 Callback return can not be considered as main function's return. – Sumeet Gohil Commented Apr 5, 2019 at 9:55
-
do you know a workaround with this? I tried creating a variable inside the main function
var solution = "text"
then inside the callback, Ireturn solution = 'The solution is: ' + results[0].solution
. But it is still not working – bradrar Commented Apr 5, 2019 at 10:15 - did you checked my answer ? do you know how to use Promises ? – Sumeet Gohil Commented Apr 5, 2019 at 10:41
- I checked your answer. I don't know how to use Promises. Sorry for that. I will search on how to use it. Thank you for your answer – bradrar Commented Apr 5, 2019 at 10:55
3 Answers
Reset to default 5Since Hapi v17, all route handler should explicitly return something.
As you can see in your error, you don't return any value because you return something in a callback of an asynchronous function which is not in the main handler function.
You have several options to deal with this, the first one is to convert your route handler to async
and use await
for asynchronous functions like so:
handler: async function (request, h) => {
const result = await connection.query('SELECT 1 + 1 AS solution')
return result // do something with sql result then return it;
}
Note: This only work if your connection.query
return a Promise instead of a NodeJS callback style. If it does not, you may take a look at utils.promisify
to convert a function with a callback to a Promise or manually by wrapping your function in a new Promise
yourself.
However if you don't want or can't use await/async, you can still convert the function with a callback to a Promise and then return the Promise but this can lead to a lot of then chaining.
const { promisify } = require('util');
[...]
handler: function (request, h) {
const query = promisify(connection.query);
return query('SELECT fancy SQL')
.then(result => {
// do something with sql result
return result
})
}
The return value of your route will be the last return value of the last .then
.
Try using async/await
server.route({
method: 'GET',
path: '/hello',
handler: async function (request, h) {
try {
const { credentials, artifacts } = await request.server.auth.test('default', request);
return { status: true, user: credentials.name };
}
catch (err) {
return { status: false };
}
}
});
I solved this problem by converting my callback to a promise.
handler: (request, h) => {
return new Promise ((resolve, reject)=> {
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
let solution = 'The solution is: ' + results[0].solution
let view = () => {
return h.view('landing-page', {solution: solution});
}
return resolve(view())
});
})
}
My problem is not hapi or mysql specific, rather my skill in javascript.