简体   繁体   中英

res.render not displaying data

I have a function that structures email addresses in different formats and checks their validity. Everything works on the server-side, however I am struggling to display the results within my Jade template. Everything looks correct so I am unsure what is going wrong..

Jade

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

Express (sorry in advance about this nested nightmare. Everything works except for the res.render towards the bottom)

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

Client-side JS

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

The anticipated behaviour is that each email address will return as true or false and display the results in each corresponding div

The issue was within the GET request on the client-side, I had not finished writing it. From there I realised I was using res.render instead of res.send which was returning an entire template rather than the object I required.

Finished and working code:

$('#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);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM