res.render不显示数据

时间:2016-04-21 14:25:35

标签: javascript node.js express pug

我有一个以不同格式构建电子邮件地址并检查其有效性的函数。一切都在服务器端工作,但我很难在我的Jade模板中显示结果。一切看起来都正确所以我不确定出了什么问题..

  #firstLast !{resultA}
  #firstDotLast !{resultB}
  #fInitialLastName !{resultC}
  #fInitialDotLastName !{resultD}
  #firstNameLInitial !{resultE}
  #firstNameDotLInitial !{resultF}
  #firstNameOnly !{resultG}
  #lastNameOnly !{resultH}
  #firstNameUnderscoreLastName !{resultI}
  #fInitialUnderscoreLastName !{resultJ}
  #firstNameUnderscoreLInitial !{resultK}

快递(提前抱歉这个嵌套的噩梦。一切都有效,除了res.render到底部)

app.get('/verify', function (req, res) {
  var firstName = req.query.firstName;
  var lastName = req.query.lastName;
  var email = req.query.email;

  var fInitial = firstName.charAt(0);
  var lInitial = lastName.charAt(0);


  var firstLast = firstName + lastName + email;
  var firstDotLast = firstName + '.' + lastName + email;
  var fInitialLastName = fInitial + lastName + email;
  var fInitialDotLastName = fInitial + '.' + lastName + email;
  var firstNameLInitial = firstName + lInitial + email;
  var firstNameDotLInitial = firstName + '.' + lInitial + email;
  var firstNameOnly = firstName + email;
  var lastNameOnly = lastName + email;
  var firstNameUnderscoreLastName = firstName + '_' + lastName + email;
  var fInitialUnderscoreLastName = fInitial + '_' + lastName + email;
  var firstNameUnderscoreLInitial = firstName + '_' + lInitial + email;

  //console.log(fInitial + ' ' + lInitial)

  //console.log(firstLast);

  verifier.verify(firstLast, function( err, info ){
    if( err ) console.log(err);
    else{
      var resultA = firstLast + ':' + ' ' + info.success;
      console.log( "Info: " + info.info );

      verifier.verify(firstDotLast, function( err, info ){
        if( err ) console.log(err);
        else{
          var resultB = firstDotLast + ':' + ' ' + info.success;
          console.log( "Info: " + info.info );

          verifier.verify(fInitialLastName, function( err, info ){
            if( err ) console.log(err);
            else{
              var resultC = fInitialLastName + ':' + ' ' + info.success;
              console.log( "Info: " + info.info );

              verifier.verify(fInitialDotLastName, function( err, info ){
                if( err ) console.log(err);
                else{
                  var resultD = fInitialDotLastName + ':' + ' ' + info.success;
                  console.log( "Info: " + info.info );

                  verifier.verify(firstNameLInitial, function( err, info ){
                    if( err ) console.log(err);
                    else{
                      var resultE = firstNameLInitial + ':' + ' ' + info.success;
                      console.log( "Info: " + info.info );

                      verifier.verify(firstNameDotLInitial, function( err, info ){
                        if( err ) console.log(err);
                        else{
                          var resultF = firstNameDotLInitial + ':' + ' ' + info.success;
                          console.log( "Info: " + info.info );

                          verifier.verify(firstNameOnly, function( err, info ){
                            if( err ) console.log(err);
                            else{
                              var resultG = firstNameOnly + ':' + ' ' + info.success;
                              console.log( "Info: " + info.info );

                              verifier.verify(lastNameOnly, function( err, info ){
                                if( err ) console.log(err);
                                else{
                                  var resultH = lastNameOnly + ':' + ' ' + info.success;
                                  console.log( "Info: " + info.info );

                                  verifier.verify(firstNameUnderscoreLastName, function( err, info ){
                                    if( err ) console.log(err);
                                    else{
                                      var resultI = firstNameUnderscoreLastName + ':' + ' ' + info.success;
                                      console.log( "Info: " + info.info );

                                      verifier.verify(fInitialUnderscoreLastName, function( err, info ){
                                        if( err ) console.log(err);
                                        else{
                                          var resultJ = fInitialUnderscoreLastName + ':' + ' ' + info.success;
                                          console.log( "Info: " + info.info );

                                          verifier.verify(firstNameUnderscoreLInitial, function( err, info ){
                                            if( err ) console.log(err);
                                            else{
                                              var resultK = firstNameUnderscoreLInitial + ':' + ' ' + info.success;
                                              console.log( "Info: " + info.info );

                                              return res.render('index', {
                                              "resultA": resultA,
                                              "resultB": resultB,
                                              "resultC": resultC,
                                              "resultD": resultD,
                                              "resultE": resultE,
                                              "resultF": resultF,
                                              "resultG": resultG,
                                              "resultH": resultH,
                                              "resultI": resultI,
                                              "resultJ": resultJ,
                                              "resultK": resultK
                                           });
                                          }
                                        });
                                        }
                                      });
                                    }
                                  });
                                }
                              });
                            }
                          });
                        }
                      });
                    }
                  });
                }
              });
            }
          });
        }
      });
    }
  });
});

客户端JS

$('#verify').click(function() {
  var parameters = {
    firstName: $('#firstName').val(),
    lastName: $('#lastName').val(),
    email: $('#email').val() };
  $.get('/verify', parameters, function(data) {
    console.log('hey' + parameters);
  });
});

预期的行为是每个电子邮件地址将返回true或false,并在每个对应的div中显示结果

1 个答案:

答案 0 :(得分:0)

问题出在客户端的GET请求中,我还没写完。从那里我意识到我使用的是res.render而不是res.send,它返回的是整个模板而不是我需要的对象。

完成并正常工作的代码:

$('#verify').click(function() {
  var parameters = {
    firstName: $('#firstName').val(),
    lastName: $('#lastName').val(),
    email: $('#email').val() };
  $.get('/verify', parameters, function(data) {
    console.log(data.resultA); //Returns full body of HTML
    $('#firstLast').html(data.resultA);
    $('#firstDotLast').html(data.resultB);
    $('#fInitialLastName').html(data.resultC);
    $('#fInitialDotLastName').html(data.resultD);
    $('#firstNameLInitial').html(data.resultE);
    $('#firstNameDotLInitial').html(data.resultF);
    $('#firstNameOnly').html(data.resultG);
    $('#lastNameOnly').html(data.resultH);
    $('#firstNameUnderscoreLastName').html(data.resultI);
    $('#fInitialUnderscoreLastName').html(data.resultJ);
    $('#firstNameUnderscoreLInitial').html(data.resultK);
  });
});