1

I'm pretty new in nodeJS and came around the following problem: I defined a Function in one of my Controllers, which I want to verify a JSON-Web-Token. I used the following function:

exports.validate = function (token, cb) {
   jwt.verify(token, secretToken, cb(err, decoded));
}

But everytime I want to run it, nodeJS gives me the error, that 'err' is not defined? In the jwt-Example there's following example:

// verify a token symmetric
jwt.verify(token, 'shhhhh', function(err, decoded) {
   console.log(decoded.foo)
});

So somewhere obviously I went wrong, but I don't know what. I also have no Idea what to look for... I've done some Google searches, but nothing helped me so far.

I would love to hear from you.

Regards

//EDIT: That's how I call the function:

AuthController.validate(req.headers['api-token'], function(err, decoded){ 
if(err){ console.log('Error: ', err);
   res.status(401); next(); 
} else if(decoded) { 
   console.log('Success: ', decoded); 
   next(); 
} 
})
4
  • Why are you calling the function? Commented Jun 29, 2014 at 20:14
  • I don't understand? Which function do you mean? Like I said, I'm pretty new to nodeJS. Maybe I didn't understand the concept of callbacks correctly. How would you have done it? Commented Jun 29, 2014 at 20:16
  • There's nothing Node specific here, callbacks are a pretty common JavaScript pattern. Commented Jun 29, 2014 at 20:17
  • Sane page: impressivewebs.com/callback-functions-javascript Commented Jun 29, 2014 at 22:13

2 Answers 2

2

In the jwt-Example, the third argument to verify is a function (created using a function expression).

In your code, the third argument is the return value or calling cb(err, decoded). The cb function might return a function, but err is a variable you are passing to it.

If you want to use a function called cb then define it and then pass the function:

function cb(err, decoded) {
    // do stuff with err and/or decoded
}
jwt.verify(token, secretToken, cb);
Sign up to request clarification or add additional context in comments.

2 Comments

That's what I do. Here's how i use it: AuthController.validate(req.headers['api-token'], function(err, decoded){ if(err){ console.log('Error: ', err); res.status(401); next(); } else if(decoded) { console.log('Success: ', decoded); next(); } });
I updated my first post, to include it. I understood the concept so far, and thats why I don't understand, why the above doesnt work. As far as I see, I did everything right.
0

If you want your function to work with callback it should use it this way:

exports.validate = function (token, cb) {

    jwt.verify(token, secretToken, function (err, decoded) {
        if (err)
            return cb(err, null);

        // callback have to handle error
        return cb(null, decoded);
    }
}

4 Comments

No. There's a much simpler solution. Can you spot it?
return cb(err, decoded), right? :) So that was my mistake? I have to return the function?
@Bergi It put it in two parts, because you usually wanna do some stuff with results (here decoded) before last return.
No. I mean jwt.verify(token, secretToken, cb);, as I had not expected that the OP wants to do some stuff to the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.