I'm getting unexpected behavior from my server. It's also very odd.
I have 2 routes that do essentially the same thing, except that one detail is changed in each route.
Here is the first route:
var mailer = require("../mailer.js");
var moment = require("moment");
var riot = require("riot");
var router = express.Router();
router.post('/personType1s/:key', function(req, res, next) {
var personType1s = req.body.personType1s;
var tourny = req.body.tourny;
var personType1_keys = [];
var updates = {};
personType1s.forEach(function(person, index, arr){
// Unique key for the tournament
var personType1_key = firebase.database().ref().push().key;
personType1_keys.push(personType1_key);
var opts = {
tourny: tourny,
personType1: person,
personType1_key: personType1_key
}
console.log('in the personType1 route');
var html = riot.render(require("../views/emailTemplates/personType1Invite.tag"), opts)
mailer(person.email, html)
console.log('html is ', html);
})
// Add the invited participants to the database
Promise.all(create_invites(tourny, personType1s, personType1_keys)).then(function(){
res.send('success');
});
});
module.exports = router;
function create_invites(tourny, personType1s, personType1_keys){
var pArr = [];
var invites = {};
var updates = {};
personType1s.forEach(function(person, index, array){
var personType1_key = personType1_keys[index];
var member_obj = {};
member_obj['email'] = person.email;
member_obj['invite_key'] = personType1_key;
member_obj['name'] = person.name;
member_obj['type'] = person.type;
updates["member_tournaments/" + encodeURIComponent(person.email).replace(/\./g, '%2E') + "/" + tourny.key] = tourny;
updates["tournament_members/" + tourny.key + "/personType1s/" + personType1_key] = member_obj;
pArr.push(firebase.database().ref().update(updates, function(error){
if(error) console.log(error);
}));
})
return pArr;
}
The other route has the exact same code, except for this line:
var html = riot.render(require("../views/emailTemplates/personType1Invite.tag"), opts)
The difference is that the second route renders a different view:
var html = riot.render(require("../views/emailTemplates/personType2Invite.tag"), opts)
The issue I'm having is that when the first route is called (i.e. personType1), the route will actually execute, however, the rendered html is actually coming from the template being used in the other route (i.e. personType2). I know this is the case b/c I print a statement specific to each route (e.g. console.log("In route 1")), followed by printing the html template. The print statement validates that I'm in the correct route, and validates that the wrong html is being rendered.
In other words, route1 is called, everything executes as it should, but when riot.render(...personType1Invite.tag) is called, it's rendering personType2Invite.tag instead.
Now here's where it gets weird. This only happens after I have already used the app to call route1 AND route2. If I call route1, and route1 only, it will continue to work as intended. The moment I call route1 AND route2, it will work as intended, but every subsequent call to route1 will always render the html from the personType2 template, even though route 1 did in fact execute and the html template in that route clearly points to personType1, not personType2. Additionally, if I disable route 2 (i.e. personType2), then route 1 will always successfully render the correct template.
via skwny
No comments:
Post a Comment