Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle server methods without cache as special case #1922

Closed
hueniverse opened this issue Sep 9, 2014 · 8 comments
Closed

Handle server methods without cache as special case #1922

hueniverse opened this issue Sep 9, 2014 · 8 comments
Assignees
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@hueniverse
Copy link
Contributor

6.8.0 broke server methods that do not generate keyed values.

@hueniverse hueniverse added the bug Bug or defect label Sep 9, 2014
@hueniverse hueniverse self-assigned this Sep 9, 2014
@hueniverse hueniverse added this to the 6.8.1 milestone Sep 9, 2014
@briandela
Copy link
Contributor

@hueniverse It looks like this introduced another bug. The parameters being passed to the server method are not correct (see below) - looks like the args is getting reassigned to a function and then passed in as the argument to the serve method.

screenshot 2014-09-09 18 52 02

@hueniverse
Copy link
Contributor Author

@briandela What does your method function look like?

@briandela
Copy link
Contributor

Here is an example server method and code adding it:

// Calculate the filename without an extension.
// For example '/usr/local/my.js' -> 'my'
function fileNameWithoutExt(filename) {
    if(!filename) {
        throw new Error('filename must be specfiied');
    }
    if(filename.indexOf('/', filename.length - '/'.length) !== -1
        || filename.indexOf('\\', filename.length - '\\'.length) !== -1){
        throw new Error('filename ends with a slash and appears to be a directory');
    }
    var extension = path.extname(filename);
    var withoutExt = path.basename(filename, extension);
    return withoutExt;
}

server.method('fileNameWithoutExt', fileNameWithoutExt);

Calling it later like this:

// If the route has a prefix we use that, otherwise we take
// the filename without extension (e.g. about.js -> /about)
var prefix = routeDetails.prefix;
prefix = prefix || ('/' + server.methods.fileNameWithoutExt(filename) + '/');

In the above, filename is a string, for example, '/about.js' or '/login/facebook.js'.

It seems like fileNameWithoutExt is getting passed a function which looks like this:

function (err, result) {
    methodNext(err, result, null, { msec: timer.elapsed(), error: err });
 };

which appears to be from here https://github.com/hapijs/hapi/blob/master/lib/methods.js#L64 (with specific commit:

args[args.length - 1] = function (err, result) {
)

@hueniverse
Copy link
Contributor Author

Ok. Your method function is the problem. Yes - this used to work because of how the code was written but it was a coincidence. The function has to use the passed callback to return its value. You can't just pass it any function. This is clearly specified in the docs.

@briandela
Copy link
Contributor

Ah, I see it called out here: https://github.com/hapijs/hapi/blob/master/docs/Reference.md#servermethodmethod

I had originally created them from the details at http://hapijs.com/tutorials/server-methods which shows the next but doesn't explicitly mention that the next callback is necessary. I'll create a PR to update those docs.

Even though the previous behaviour was a coincidence/bug it may be worth putting a breakingchange label on 6.8.1 just to help if others run into this.

@nlf
Copy link
Member

nlf commented Sep 10, 2014

For the record..

The fn parameter is the actual function to call when the method is invoked. It can take any number of arguments but *must* accept a callback as its last parameter.

I can make a more prominent note earlier in the documentation though

@briandela
Copy link
Contributor

@nlf You're correct, I am wrong. Apologies for the confusion.

@nlf
Copy link
Member

nlf commented Sep 10, 2014

No worries at all, and it can certainly be more clear. I'll add a note at the top where the function is actually defined as well.

nlf added a commit to outmoded/hapijs.com that referenced this issue Sep 10, 2014
@hueniverse hueniverse added the breaking changes Change that can breaking existing code label Sep 10, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jan 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants