Here’s the code:
exports.index_post = function(req, res) {
var nicks = [];
if (req.body.nick) {
for (var nick in nicks) {
if (nick == req.body.nick) {
res.redirect("/");
} else {
nicks.push(req.body.nick)
req.session.nick = req.body.nick;
res.redirect("/msg");
console.log(nicks);
}
}
} else {
res.redirect("/");
}
};
What it’s meant to do is check if req.body.nick is one of the items in the nicks dictionary, and if if is redirect it back to the root. If it’s not in the dictionary, it should add it to the dictionary and set it as a session variable, then redirect to /msg. However, this code doesn’t seem to be working for me and instead it causes Express to hang. Could someone tell me what I’m doing wrong? Thanks.
First off, you’re creating a new
nicksarray every time the function is run; it sounds like you want this to persist throughout the life of the server, each request potentially adding to thenicksarray. Also, though you’re iterating over the array looking forreq.body.nick, the way theif/elsestatement is written inside the loop, it will always redirect on the very first iteration through the loop. Try something like this: