I am using the latest versions of Hapi Swagger, Hapi, Inert, Vision and MySQL.
I get the error above when calling a handler which then parses the request object and then calls a MySQL database.
Debug: internal, implementation, error
Error: method did not return a value, a promise, or throw an error
at module.exports.internals.Manager.execute (/Sites/api/node_modules/hapi/lib/toolkit.js:52:29)
at
at process._tickCallback (internal/process/next_tick.js:160:7)
I have stripped out all the excess from the handler to its very core, and it looks like this. These returns do get hit in terms of
exports.getData= (req, h) => {
dbPool.getConnection(function(err, connection) {
if (connection) {
connection.query(sql, function(err, result) {
if (!err && result !== undefined) {
console.log('this log is hit, but this is where the error seems to get thrown?');
return h.response(result);
} else if (result === undefined) {
return h.response(result);
}
connection.release();
});
} else {
return Boom.badImplementation();
}
});
};
You're mixing callbacks and promises, the error is quite clear, your handler doesn't return a promise, hapi 17 doesn't work with that.
Can you point me out to an example or a reference? I had a look at the migration guide
An example of what ? Converting callbacks to promises ? That's a language issue, not really hapi's.
Please appreciate the examples given on the migration documents and elsewhere were mostly just returning the h response, and not explaining that a full promise was needed.
Whilst it was a simple fix, and a simple solution, as a fellow programmer you should be more understanding in that sometimes such things aren't always clear. I don't think its clarified enough in the migration notes exactly what is needed to happen.
Once I have migrated to v17.x I will try and do a PR to clarify the documentation. In the meantime, I don't think the message was clear, as I was returning a value.
I was merely asking the question about what needed clarification or examples, because to me it was quite clear that you returned nothing from the handler and the error mentioned that. Now the stacktrace might not have helped you pinpoint the exact place where the return was missing, I'd agree on that.
The migration checklist covered that part in Lifecycle methods, I'm not sure I see an issue here.
Documentation PRs are always welcome but remember that hapi's documentation doesn't aim to be a documentation for javascript itself, there are many places explaining the use of promises or async/await in a callback context and I don't think hapi's documentation should do that, could eventually use some more examples, but it's hard because each case is different, it's important you know what you're doing and not blindly follow a pre-written guide, as the libraries you use might already support promises.
Lol @ the thumbs down. Hapi 17 expects a promise from the handler.
You also need to learn to use async/await.
Here's what you're actually looking for:
exports.getData= (req, h) => {
// Pretty sure this is what he means
return new Promise((resolve, reject) => {
dbPool.getConnection(function(err, connection) {
if (connection) {
connection.query(sql, function(err, result) {
if (!err && result !== undefined) {
return resolve(result);
} else if (result === undefined) {
return resolve(result);
}
connection.release();
});
} else {
reject(Boom.badImplementation());
}
});
});
};
Here's what you need to start doing:
exports.getData = async (req, h) => {
const connection = await dbPool.getConnection();
if (connection) {
try {
const results = await connection.query(sql);
connection.release();
return results;
}
catch (e) {
throw new Boom(e);
}
} else {
return Boom.badImplementation();
}
});
};
In both instances, handler returns a promise.
Most helpful comment
Lol @ the thumbs down. Hapi 17 expects a promise from the handler.
You also need to learn to use async/await.
Here's what you're actually looking for:
Here's what you need to start doing:
In both instances,
handlerreturns a promise.