tempusfugit (121) [Avatar] Offline
#1
Given:
var db = {users: [ { name: 'tobi' }, { name: 'loki' }, { name: 'jane' } ] };

var user = db.users[match[1]]; // won't ever find anything
match[1] is a string (e.g. 'tobi') and db.users is an array of objects (like {name:'tobi'}). Possible work around (as user objects may have other properties):

function matchUserName (user,i,all) {
....if(user.name !== this.username) return false;

....this.user = user; // user found
....return true;
};

function users(req, res, next) {
....var match, context, err;
....match = req.url.match(/^/user/(.+)/);
....if (match) {
........context = {username: match[1]};
........if (db.users.some(matchUserName,context)) {
............res.setHeader('Content-Type', 'application/json');
............res.end(JSON.stringify(context.user));
449087 (1) [Avatar] Offline
#2
I had the same issue. Solved with this:
function users(req, res, next){
	var match = req.url.match(/^\/user\/(.+)/);
	var user;
	if(match){
		for (var i = users.length - 1; i >= 0; i--) {
			if(db.users[i].name == match[1]){
				user = db.users[i];
			}
		}
		if (user){
			res.setHeader('Content-Type', 'text/plain');
			res.end(JSON.stringify(user));
		} else {
			var err = new Error('User not found');
			err.notFound = true;
			next(err);
		}
	} else {
		next();
	}
}