最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hapi handler method is not returning a value - Stack Overflow

programmeradmin1浏览0评论

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, I return 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
Add a ment  | 

3 Answers 3

Reset to default 5

Since 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.

发布评论

评论列表(0)

  1. 暂无评论