服务器

时间:2017-04-28 06:29:27

标签: node.js express

我的服务器出现意外行为。这也很奇怪。

除了在每条路线中更改了一个细节外,我有2条基本相同的路线。

这是第一条路线:

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;
}

另一条路线的代码完全相同,但此行除外:

var html = riot.render(require("../views/emailTemplates/personType1Invite.tag"), opts)

不同之处在于第二条路线呈现不同的视图:

var html = riot.render(require("../views/emailTemplates/personType2Invite.tag"), opts)

我遇到的问题是,当第一个路由被调用时(即personType1),路由将实际执行,但是,渲染的html实际上来自在另一个路径中使用的模板(即personType2)。我知道这是b / c的情况我打印一个特定于每个路由的语句(例如console.log(“路由1”)),然后打印html模板。 print语句验证我的路径是否正确,并验证正在呈现错误的 html。

换句话说,调用route1,一切都按预期执行,但是当调用riot.render(...personType1Invite.tag)时,它会呈现personType2Invite.tag

现在这里变得奇怪了。这只发生之后我已经使用该应用调用route1和route2。如果我只调用route1和route1,它将继续按预期工作。当我调用route1和route2时,它将按预期工作,但是对route1的每次后续调用将始终从personType2模板呈现html,即使路由1确实执行并且该路由中的html模板明确指向personType1,不是personType2。此外,如果我禁用路由2(即personType2),则路由1将始终成功呈现正确的模板。

修改 这是第二条路线的代码:

var mailer = require("../mailer.js");
var moment = require("moment");
var riot = require("riot");
var router = express.Router();
// var tag = require("../views/emailTemplates/personType2Invite.tag");

router.post('/:key', function(req, res, next) {
  // {name, email, type}
  var personType2s = req.body.personType2s;
  // See body of file for tourny object structure
  var tourny = req.body.tourny;
  var personType2_keys = [];
  var updates = {};

  personType2s.forEach(function(person, index, arr){
    // Unique key for the tournament
    var personType2_key = firebase.database().ref().push().key;
    personType2_keys.push(personType2_key);
    var opts = {
      tourny: tourny,
      personType2: person,
      personType2_key: personType2_key
    }
    var html = riot.render(require("../views/emailTemplates/personType2Invite.tag"), opts)
    mailer(person.email, html)
  })

  // Add the invited participants to the database
  Promise.all(create_invites(tourny, personType2s, personType2_keys)).then(function(){
    res.send('success');
  });

});
module.exports = router;


function create_invites(tourny, personType2s, personType2_keys){
  var pArr = [];
  var invites = {};
  var updates = {};
  personType2s.forEach(function(person, index, array){
    var personType2_key = personType2_keys[index];
    var member_obj = {};    
    member_obj['email'] = person.email;
    member_obj['invite_key'] = personType2_key;
    member_obj['name'] = person.name;
    member_obj['type'] = person.type;

    // i.e. member_tournaments/<email_address>/tourny_key
    updates["member_tournaments/" + encodeURIComponent(person.email).replace(/\./g, '%2E') + "/" + tourny.key] = tourny;
    updates["tournament_members/" + tourny.key + "/personType2s/" + personType2_key] = member_obj;

    pArr.push(firebase.database().ref().update(updates, function(error){
      if(error) console.log(error);
    }));
  })

  return pArr;
}

0 个答案:

没有答案