I'm using this ui template to display some Grafana charts in an iframe.
The flow works OK, but on page refresh I see
TypeError: Cannot read properties of undefined (reading 'payload')
in browser console. Is there some way to stop this ?
My limited understanding tells me that scope watch shouldn't be trying to set the attribute until the payload arrives, so why the error on refresh or page load?
<html>
<iframe id="myFrame3" width="1384" height="106px" style = "overflow:hidden; margin:auto; border:0px solid green;"></iframe>
<script>
//NOTE, more then one chart on a page, rename in the second and following
//templates the "myframe" variable so that every template has a unique name.
(function(scope) {
// watch msg object from Node-RED
scope.$watch('msg', function(msg) {
// new message received
var x = document.getElementById('myFrame3');
x.setAttribute('src', msg.payload);
});
})(scope);
</script>
</html>
what if you console.log the msg in $watch to see for any clues in the browser's dev tools, where that msg is coming from
scope.$watch('msg', function(msg) {
// new message received
console.log(msg)
var x = document.getElementById('myFrame3');
x.setAttribute('src', msg.payload);
});
I tested your code and it does seem to execute everytime .. even with the Reload last value unchecked
maybe the best way to block it from proceeding is use an if statement like in the example in the help tab
to check if the msg has a value before proceeding
(function(scope) {
// watch msg object from Node-RED
scope.$watch('msg', function(msg) {
if (msg) {
// new message received
var x = document.getElementById('myFrame3');
x.setAttribute('src', msg.payload);
}
});
})(scope);