-1

I have an error typeError: targetProperties[targetId][0].destroy is not a function

the code is

var clearSelectedTarget = function(targetId) {
    try {
        if (typeof targetProperties[targetId] !== "undefined") {
            typeof targetProperties[targetId][0] !== "undefined"? targetProperties[targetId][0].destroy():"";
            typeof targetProperties[targetId][1] !== "undefined"? targetProperties[targetId][1].destroy():"";
            delete targetProperties[targetId];
        }
        if (typeof cellProperties[targetId] !== "undefined") {
            cellProperties[targetId][0].destroy();
            delete cellProperties[targetId];
        }
    } catch (ex) {
        $1.error("[ol.js-clearSelectedTarget] Error.", ex);
        return;
    }
};

Help me to solve this error

  • 1
    Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). – Sebastian Simon Nov 01 '22 at 09:05
  • check if `targetProperties[targetId][0]` has `destroy()` method. – Layhout Nov 01 '22 at 09:06
  • We've no clue what is the value of `cellProperties[targetId][0]`. `destroy` is not a known method in JS or DOM (unless you're working with [XRCompositionLayers](https://developer.mozilla.org/en-US/docs/Web/API/XRCompositionLayer/destroy)), In order to use it, you've to create the method to the data type of `cellProperties[targetId][0]`. – Teemu Nov 01 '22 at 09:06
  • @Layhout Obviously it doesn't have that method, that's why the error occurs. – Teemu Nov 01 '22 at 09:11

1 Answers1

0

first, you should provide more info about what are you trying to do so people could help you,

from what I understand from your code you are trying to call destroy() method and it throws an error, this is what I think you should do instead of checking if the target type is undefined check if the method you are trying to call is a function like this

// this will not throw an error if destroy doesn't exist
typeof targetProperties[targetId][0].destroy === "function"? targetProperties[targetId][0].destroy():"";
Super sub
  • 269
  • 2
  • 8