Why did this comment make a difference?

Bruce Gust
Bruce Gust used Ask the Experts™
This is my "is-auth.js" file:

const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
	//console.log(req.get(Authorization));
	const authHeader = req.get('Authorization');
	if(!authHeader) {
		const error = new Error('Not Even Working');
		error.statusCode = 401;
		throw error;
	} else {
		const token = authHeader.split(' ')[1];
		let decodedToken;
		try {
			decodedToken = jwt.verify(token, 'secret');
		} catch (err) {
			err.statusCode = 500;
			throw err;
		}
		if(!decodedToken) {
			const error = new Error('Not Authenticated');
			error.statuscode = 401;
			throw error;
		}
		req.userID = decodedToken.userId;
		next();
	}
};

Open in new window


If you notice the line, "//console.log(req.get(Authorization));," - if I un-commented that line, I would get an error that said, "message: "Authorization is not defined."

I'm thinking that's because the "console.log..." thing can't be referenced in the code the way that I have it. And what makes it hard to "see," is that unless you're aware of how a method needs to be crafted and WHERE you can actually do a "console.log," you're going to beat your head up against the way looking for another error.

Am I correct?

Is the placement of "console.log(req.get(Authorization))" toxic? Is that a dealbreaker? Will that break the code?

And what's a good rule of thumb for "console.log" as far as where you can put it within a method and not be writing code that's going to break however sound it might otherwise be?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
//console.log(req.get(Authorization)); // you miss the single quotes here so you get an error HERE and not on the next line
const authHeader = req.get('Authorization'); // you did not miss the single quotes

const authHeader = req.get('Authorization');
console.log(authHeader);

Open in new window

Bruce GustPHP Developer

Author

Commented:
The things we learn!

Thanks, Leak!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial