渲染变量到EJS模板?

时间:2019-03-09 23:23:46

标签: node.js express mqtt ejs

我的ejs端(网页)每次刷新页面时都会更新错误的变量,但是每个变量都有不同的名称。我不知道怎么了。

我的index.js正在使用MQTT从esp8266接收消息,然后将其呈现给ejs。

index.js

var topicTemp = "FromESPtoWeb/temp";
var topicMessagemoisture = "FromESPtoWeb/moisture";

var content = { doorMsg: "Door Closed" ,
                windowMsg: "Window Closed",
                tempMsg : "",
                moistureMsg : "" ,
                motionMsg: "Motion" };

 client.on('connect', function () {

    //Subscribe to topic "FromESPtoWeb/temp"
    client.subscribe(topicTemp, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }

        client.on('message', function (topicTemp, temp) {
            console.log(temp.toString());
            content.tempMsg = temp.toString();
        });

    }) 
  //Subscribe to topic "FromESPtoWeb/moisture"   
   client.subscribe(topicMessagemoisture, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }

        client.on('message', function (topicMoisture, moisture) {
            console.log("new message on " + topicMoisture + " - " + 
            moisture.toString());
            content.moistureMsg = moisture.toString();
        });
    })

})

/* GET home page. */
router.get('/', function(req, res) {
    res.render('index', {  content : content } );
});

index.ejs

<h4> <%= content.moistureMsg %> </h4>
<h4> <%= content.motionMsg %> </h4>
<h4> <%= content.windowMsg %> </h4>
<h4> <%= content.doorMsg %> </h4>

content.moistureMsg 有时会显示假定为 content.windowMsg 的内容,或者 content.doorMsg 会显示假定的值成为 content.motionMsg 。一团糟...

2 个答案:

答案 0 :(得分:0)

使用请求对象!。

router.get('/', function(req, res) { 
    res.render('index', { content : req.content } ); 
});

答案 1 :(得分:0)

我对client.on和subscription的理解非常错误。我重建了整个代码,现在可以正常工作了。

var topicTemp = "FromESPtoWeb/temp";
var topicDoor = "FromESPtoWeb/door";
var topicWindow = "FromESPtoWeb/window";
var topicMoisture = "FromESPtoWeb/moisture";
var topicMotion = "FromESPtoWeb/motion";

var content = { doorMsg: "Door Closed" , windowMsg: "Window Closed", tempMsg:"", moistureMsg:"", motionMsg: ""};



client.on('connect', function () {

    client.on('message', function (topic, message) {
        if(topic === topicTemp) {
            temp(message);
        }
        if(topic === topicDoor) {
            door(message);
        }
        if(topic === topicWindow) {
            window(message);
        }
        if(topic === topicMoisture) {
            moisture(message);
        }
        if(topic === topicMotion) {
            motion(message);
        }

    });

    client.subscribe(topicTemp, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }
    });
    client.subscribe(topicDoor, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }
    });
    client.subscribe(topicWindow, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }
    });
    client.subscribe(topicMoisture, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }
    });
    client.subscribe(topicMotion, function (err) {
        if (err) {
            alert("something went wrong on subscribe to message");
        }
    });


});

var temp = (message) => {
    console.log(message.toString());
    content.tempMsg = message.toString();
}
var door = (message) => {
    if (message == "Door Open") {
        console.log("Door open");
        content.doorMsg = message;
    }else if (message == "Door Closed") {
        console.log("Door closed");
        content.doorMsg = message;
    }
}
var window = (message) => {
    if (message == "Window Open") {
        console.log("window open");
        content.windowMsg = message;
    }else if (message == "Window Closed") {
        console.log("window closed");
        content.windowMsg = message;
    }
}
var moisture = (message) => {
    console.log(message.toString());
    content.moistureMsg = message.toString();
}
var motion = (message) => {
    console.log(message.toString());
    content.motionMsg = message.toString();
}
/* GET home page. */
router.get('/', function(req, res) {
    res.render('index', {  content : content } );
});