RED.httpAdmin.get not refreshing JSON

#1

Hi,

I´m using httpAdmin.get to send a json from the runtime js to the html. The problem I have is that the json is only updated the first time the code runs. I can see in the console that data variable is being updated, but when I see the json (opening the url with Chrome), the information is not being updated. It is refreshed only the first time after restarting Node-Red.
I think that maybe the res.json() is not able to over-write the existing json, but I don't know what to use instead.
Can anyone please help me?

mycostumnode.js:

node.updateEquipmentList = setInterval( async function () {
            var data={};
            if (typeof node.server !== 'undefined' && node.server && typeof node.server.getEquipmentsModel === 'function'){
                data = await node.server.getEquipmentsModel();
                RED.httpAdmin.get("/equipments/:id", RED.auth.needsPermission('TimeSeries.read'), function(req,res) {
                    res.json(data);
                });
                console.log(JSON.stringify(data));
            }
        }, 10000);
0 Likes

#2

Hi @germancmartinez - when sharing code on the forum please follow the advice in this post: How to share code or flow json. I have edited your post to fix the formatting.

You are creating an interval that runs every 10 seconds to update some data. The problem comes that you add a new route handler to RED.httpAdmin every time. That is not a good idea. You should separate those things out:

  • add one route handler that returns whatever is currently in data
  • create an interval to update data as needed

var data = {};
RED.httpAdmin.get("/equipments/:id", RED.auth.needsPermission('TimeSeries.read'), function(req,res) {
   res.json(data);
});

node.updateEquipmentList = setInterval( async function () {
    var data={};
    if (typeof node.server !== 'undefined' && node.server && typeof node.server.getEquipmentsModel === 'function'){
      data = await node.server.getEquipmentsModel();
      console.log(JSON.stringify(data));
    }
}, 10000);

Finally, make sure you clear the node.updateEquipmentList interval in your node's close handler.

1 Like

#3

Thank you so much! Now it's working!

0 Likes