0

So I've been encountering this error quite a bit lately and it looks like this...

Main.js:84 Uncaught TypeError: Cannot set property 'CurrParent' of undefined

And basically I have a couple draggable elements all of which have an attribute called "c_index" which is set to an integer, and what i'm doing with them is using them in another file which is going to act as some sort of a metadata file, and that's where the problem is: I'm storing the "c_index" of the element currently being dragged in a var called CI it looks like this:

var CI = event.relatedTarget.getAttribute("c_index");

and i'm using it like this:

Meta.elements.CI.CurrParent = 'workspace';

and the object that i'm trying to save into looks like this without editing:

var Meta = { 
    elements : { 
    } 
} 

Any help would be much appreciated :)
p.s: I'm using a framework called interact.js

5
  • 1
    That's not "weird". It just means that Math.elements.CI is undefined. Commented Jan 26, 2017 at 14:18
  • Your var CI = ... code won't magically produce a CI property on Meta.elements. You'd want Meta.elements.CI = event.relatedTarget.getAttribute("c_index");. But note that that property will have a primitive string as a value. Setting a property on that primitive string later (Meta.elements.CI.CurrParent = ...) is a long-winded no-op (it won't be kept). It would temporarily create a string object, set the property on the string object, and then throw the string object away, with no effect at all on Meta.elements.CI. Commented Jan 26, 2017 at 14:19
  • @T.J.Crowder you almost got it but i want it to create a new child object that is called whatever value CI outputs that I can then write into. Commented Jan 26, 2017 at 14:29
  • Sorry, I don't know what you mean there... Commented Jan 26, 2017 at 14:33
  • @T.J.Crowder No problem, I understand my wording is all over the place, what i wanted to say is, I have a file, which by default has two nested objects the outer one being called Meta and the inner one being called elements, what i want to do is dynamically create another object called l1 (which corresponds to the c-index in this case) inside of elements. Commented Jan 26, 2017 at 14:39

1 Answer 1

0

From a comment on the question:

...what i want to do is dynamically create another object called l1 (which corresponds to the c-index in this case) inside of elements

If I'm understanding you correctly, you'd want to use the value of c_index as the property name, via brackets notation. If you want that object to have a CurrParent property with the value "workspace", you can initialize that at the same time:

var ci = event.relatedTarget.getAttribute("c_index");
Meta.elements[ci] = {CurrParent: "workspace"};
1
  • Thanks for the answer, you really helped me out in the long run! Commented Jan 26, 2017 at 14:48

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.