node-red/node-red-nodes

SNMP "ReferenceError: response is not defined."

echobops opened this issue · 3 comments

Trying to use SNMP Subtree, but am getting a reference error here:

response.push({ oid: varbinds[i].oid, value: varbinds[i].value });

Same thing with SNMP Walk

response.push({ oid: varbinds[i].oid, value: varbinds[i].value });

Can you share the details of the errors ?
Which version of the node do you have installed and which version of nodejs? And how do you have the node configured ?

Ah. Poking around, "response" is defined below that function. Moving it to the top of the on input function resolves the issue. I'm just polling one oid on one host, v1 snmp. I've verified the credentials by using snmpwalk in the cli.

node-red v3.0.2
node-red-node-snmp v1.0.2

       node.on("input", function (msg) {
            const oids = node.oids || msg.oid;
+ ADDED     const response = [];  
            const { host, sessionid, user, options } = prepareSnmpOptions(node, msg);
            function feedCb(varbinds) {
                for (let i = 0; i < varbinds.length; i++) {
                    if (SNMP.isVarbindError(varbinds[i])) {
                        node.error(SNMP.varbindError(varbinds[i]), msg);
                    } else {
                        response.push({ oid: varbinds[i].oid, value: varbinds[i].value });
                    }
                }
            }
            if (oids) {
                msg.oid = oids;
                let sess = openSession(sessionid, host, user, options);
                sess.on("error", function (err) {
                    node.error(err, msg);
                })
                //move response array & feedCb to inside `node.on("input",` to avoid subsequent
                // calls overwriting results from previous operations (each call gets own result/response)
- REMOVED       // const response = []; 

Doing that on both walk and subtree functions seems to work.

yes - would make total sense - Patched and released as v1.0.3