【问题标题】:node.js and ejs variable not showing at page refreshnode.js 和 ejs 变量在页面刷新时未显示
【发布时间】:2018-06-03 20:56:16
【问题描述】:

我正在尝试将变量从 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);
                  });
            });   


        });

    });

【问题讨论】:

    标签: javascript node.js firebase ejs


    【解决方案1】:

    解决方案:

    我为每个 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);
                  });
            });   
    
    
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多