最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 7浏览 0评论

node.js和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);
                  });
            });   


        });

    });
回答如下:

解:

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


    });

});

node.js和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);
                  });
            });   


        });

    });
回答如下:

解:

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


    });

});
发布评论

评论列表 (0)

  1. 暂无评论