node.js和ejs变量在页面刷新时未显示

时间:2017-12-21 08:10:10

标签: javascript node.js firebase ejs

我正在尝试将变量从js传递到.ejs模板文件。这些值将在页面首次打开但未在刷新页面显示。当我记录变量时,每次刷新时它们都会出现在控制台中。

EJS:

<input type="text" value="<%= userfb.testTarget %>" data-min="<%= userfb.testMin %>" data-max="<%= userfb.testMax %>" data-fgcolor="#157fa2" class="dial ringtarget">

app.js

var query = firebase.database().ref('/doctors/patients/' + request.id + "/testResults");
    query.once("value")
      .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var resultData = childSnapshot.val();
            var testid= resultData.testid;
             console.log("testid:"+testid);

            //Setting Test Results
            // Loop through users in order with the forEach() method. The callback
            // provided to forEach() will be called synchronously with a DataSnapshot
            // for each child:
                  var childData = childSnapshot.val();
                  console.log(childData.testResultValues);
                  var testResultValues =[];
                  for (i in childData.testResultValues) {
                    testResultValues.push(childData.testResultValues[i].value);
                  }
                  userfb.testResultValues=testResultValues;
                  console.log(testResultValues);        


            //Getting test informations      
            var testquery = firebase.database().ref('/doctors/tests').orderByChild("testID").equalTo(testid);
            testquery.once("value")
              .then(function(snapshot2) {
                  snapshot2.forEach(function(snapshot2) {
                      var testData = snapshot2.val();
                      userfb.testName=testData.testName;  
                      userfb.testMax=testData.limitValues.max; 
                      userfb.testMin=testData.limitValues.min; 
                      userfb.testTarget=testData.normalValues.Female; 
                      console.log("testmax:"+userfb.testMax);
                  });
            });   


        });

    });

1 个答案:

答案 0 :(得分:0)

解决方案:

我为每个firebase查询划分了返回值(作为userfb和testInformations),现在使用了刷新页面。

var query = firebase.database().ref('/doctors/patients/' + request.id + "/testResults");
query.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
        var resultData = childSnapshot.val();
        var testid= resultData.testid;
         console.log("testid:"+testid);

        //Setting Test Results
        // Loop through users in order with the forEach() method. The callback
        // provided to forEach() will be called synchronously with a DataSnapshot
        // for each child:
              var childData = childSnapshot.val();
              console.log(childData.testResultValues);
              var testResultValues =[];
              for (i in childData.testResultValues) {
                testResultValues.push(childData.testResultValues[i].value);
              }
              userfb.testResultValues=testResultValues;
              console.log(testResultValues);        


        //Getting test informations      
        var testquery = firebase.database().ref('/doctors/tests').orderByChild("testID").equalTo(testid);
        testquery.once("value")
          .then(function(snapshot2) {
             testInformations = snapshot2.val()
              snapshot2.forEach(function(snapshot2) {
                  var testData = snapshot2.val();
                  testInformations.testName=testData.testName;  
                  testInformations.testMax=testData.limitValues.max; 
                  testInformations.testMin=testData.limitValues.min; 
                  testInformations.testTarget=testData.normalValues.Female; 
                  console.log("testmax:"+testInformations.testMax);
              });
        });   


    });

});