1

This is the code that I currently have, one problem that is happening is I cannot use test() because presets[index].name and value are not visible outside of their function scope, how should I declare my array of objects in the global scope in order for me to be able to access these two variables in other functions?

var presets = [];
var index;

function CreatePresetArray(AMib, AVar) {
    var parentpresetStringOID = snmp.getOID(AMib, AVar);
    var presetStringOID = parentpresetStringOID;
    parentpresetStringOID = parentpresetStringOID.substring(0, parentpresetStringOID.length - 2);
    log.error("parentpresetStringOID is " + parentpresetStringOID);

    var presetswitches = {};

    for (var i = 1; i < 41; i++) {
        presets.push(presetswitches);
        try {
            log.error("presetStringOID before getNextVB= " + presetStringOID);
            vb = snmp.getNextVB(presetStringOID);
            presetStringOID = vb.oid;
            log.error("presetStringOID  after getnextVB= " + presetStringOID);

            var presetStringVal = snmp.get(presetStringOID);

            log.error("presetStringVal= " + presetStringVal);

            index = i - 1;

            presets[index].name = presetStringOID;
            presets[index].value = presetStringVal;
            log.error("preset array's OID at position [" + index + "]     is" + presets[index].name + " and the value stored is " + presets[index].value);

            //log.error("presets Array value ["+index+"] =     "+presets[index].configs);

            if (presetStringOID.indexOf(parentpresetStringOID) != 0) {
                break;
            }

        } catch (ie) {
            log.error("couldn't load preset array " + index);
        };
    };
}

CreatePresetArray(presetMib, "presetString");

function test() {
    for (i = 1; i < 41; i++) {
        log.error("test" + presets[index].name + "        " + presets[index].value);
    };
}
test();
7
  • Please paste the code you mention Commented Oct 22, 2013 at 19:23
  • take your time, indent every code line at least 4 characters ... and it will work Commented Oct 22, 2013 at 19:25
  • You can also post a link to jsfiddle.net/ or codepen.io Commented Oct 22, 2013 at 19:27
  • Where and how exactly do you call the function CreatePresetArray? Commented Oct 22, 2013 at 19:29
  • Which means "if you don't call it at all, that would be the problem" :-) Commented Oct 22, 2013 at 19:30

2 Answers 2

2

The for loop in your function test iterates over i but uses index inside the loop. Perhaps you meant to use

for (i = 0; i < 40; i++) { // 1 lower as you were using `index = i - 1` before
    log.error("test" + presets[i].name + "        " + presets[i].value);
}

Re-wrote your code. I don't think I made that much by way of change. If this doesn't clear up your problem, consider: Is the catch happening each iteration? Is the problem actually coming from a different method which is only visible here? Also, consider logging the whole presets Array when debugging to see what it looks like.

var presets = [];

function CreatePresetArray(AMib, AVar) {
    var parentPresetOID, presetOID, presetValue, preset, vb, i;
    parentPresetOID = snmp.getOID(AMib, AVar);
    presetOID = parentPresetOID; // initial
    parentPresetOID = parentPresetOID.substring(0, parentPresetOID.length - 2);

    log.error("parentPresetOID is " + parentPresetOID);
    presets = []; // empty array in case not already empty
    for (i = 0; i < 40; ++i) {
        try {
            preset = {}; // new object
            // new presetOID
            vb = snmp.getNextVB(presetOID);
            presetOID = vb.oid;
            log.error("presetOID  after getnextVB= " + presetOID);
            // new value
            presetValue = snmp.get(presetOID);
            log.error("presetValue= " + presetValue);
            // append data to object
            preset.name = presetOID;
            preset.value = presetValue;
            // append object to array
            presets.push(preset);
            // more logging
            log.error(
                "preset array's OID at position [" + i + "]" +
                "     is" + presets[i].name + " and " +
                "the value stored is " + presets[i].value
            );
            if (presetOID.indexOf(parentPresetOID) !== 0) {
                break;
            }
        } catch (ie) {
            log.error("couldn't load preset array " + i);
            if (presets.length !== i + 1) { // enter dummy for failed item
                presets.push(null);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

thank you, I tried to use i instead of index, but it still can't see the .name and .value of array presets, because it was only declared in function createpresetArray
No, it wasn't. You declared it before the function. That should be sufficient, if you call the function CreatePresetArray before you call test()
I tried calling createpresetArray before calling test(), when I compile, the error says cannot read property "name" from undefined
Please try again with my edited code, the error may have been appearing only on the last iteration
thank you Paul, I tried with your code, but still no luck, it gives the same error as before.
|
1

Two options come to mind immediately:

  • you could pass the preset array as a argument to test().
  • You could put both CreatePresetArray() and test() inside a wrapper function and declare preset array at the top of your wrapper. That would give them both access to the variable.

It's generally considered Bad Form to declare globals if it can be avoided. Pollutes the namespace.

2 Comments

presets is vard before the CreatePresetArray declaration.
the thing is from what I was told, I am suppose to create two different functions, the first one which is createpresetArray, and the second one is suppose to be able to refer to the array that I created by giving it an index, this means that both functions shouldn't be wrapped in a wrapper, and I was also told that the second function which refer to the array I created shouldn't take the array as an argument but simply an index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.