Cannot read property 'variable' of undefined - javascript

I am navigating an object that contains an array of objects.
When I use chrome's js developer console I can grab the title property from the first item in the array i.e.
hello.example.array[0].title
this returns the title (only in the js developer console). However when I write a script to do this for me suddenly I receive this response:
Cannot read property 'array' of undefined
here is an example of my js
var theTitle = hello.example.array[0].title;
console.log(theTitle);
Why does the console find it correctly when my js does not?

Try selecting the expression in question in the script view and then use the Ctrl-Shift-E shortcut to evaluate in the console. Or, copy-and-paste from the script view into the console. Or, hover over the last component of the expression in the script view to see the value. In either case, you will most likely find that you've mistyped something, or are executing the script in a different context than that in which you are evaluating the expression in the console, etc.

Thanks to #Barmar, I realized I needed to view how I was going about grabbing the 'title' property.
My solution was in the context of other code and how the object was being created in the first place.
Thanks all for the help!
Edit 6 Years later:
This post has some popularity so I thought I would update it with a more valid answer...
Make sure your JavaScript actually has the scope/context of the object to begin with.
The JS Console is able to return it because it is being executed after the code has run.. whereas the JavaScript in question might be trying to access the variable before it is set.
So check if you have the right scope/context and that the code is not trying to access a variable before it has been set.

Related

TypeError: this.ExportDataObject is not a function

I have a simple pdf file containing an embedded file (test.xml) I'm trying to add a JS to call it once the pdf file is opened (even with notification to user to accept the risk etc). I've read that to perform that, the JS that should be used is this:
this.ExportDataObject({cName:"test.xml", nLaunch:2});
For some reason, it is not working. I checked the debug js console on my Acrobat reader DC (version 2021.001.20145) the the error shown is TypeError: this.ExportDataObject is not a function. I'm not sure why on my "this" object the ExportDataObject is not available... I think it should be available always, shouldn't it? I also tested without the this. and the error is different ReferenceError: ExportDataObject is not defined.
That makes to think to me that this.ExportDataObject is existing but is not a function as the original error said... but, if is not a function, what is? a typeof is showing "undefined". Not sure how to make this work. Not sure if next steps should more JS debugging or if the problem is related to something on pdfs or Acrobat. Any help? thanks.
Javascript function names are case-sensitive and as documented by Adobe (p. 151), the correct spelling is exportDataObject() without the leading capitalization.
I believe you misspelled ExportDataObject()
It should be exportDataObject()
Using Javascript you should be careful as it is easy to mess up spelling as JS will interpret that in different ways.
As like most of the languages, js is also case sensitive.
But ReferenceError: ExportDataObject is not defined, ReferenceError always states that the object is not defined at all, and could'nt be found among the class methods.
so you need to make sure the function with the exact exportDataObject name is present and use them accordingly.

Excel Javascript Add-in - Access to property denied

I am currently working on a Excel Javascript Add-In that is supposed to go through an extremely big amount of Data to take a value from one column, seperate it into certain strings and paste those strings into seperate columns.
My problem is with accessing the value of the cell.
I am using the standard procedure (see below) which is also used in the sample code given by Visual Studio (Community 2017).
When I print all the possible properties of the given cell into the console, some have a note next to them that says: "" which is German for ""
Therefore I can´t access these properties and not get any result for the "value" property.
The strange thing is that the same code already worked in another project and therefore another project.
The code is basically
var sheet = context.workbook.worksheets.getActiveSheet();
var range = sheet.getRange("C1:C1").load("values,text");
var value = range.values[0][0];
The property notes:
I received the following error code:
Error: PropertyNotLoaded: The property 'values' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.
I searched the internet a lot and tried to find an awnser but I couldn´t find anything. I hope you guys can help me.
Thank you very much!

Numeric keys in object gives error in Firefox ("SyntaxError: missing ; before statement")

The object {'1':'test'} gives an error in Firefox, but seems to be fine in Chrome. Does anyone know how to get around this error and make this work? The keys and values are from an external source so I can't just change them. (Run the code snippet below in Firefox and you will see the error.)
{'1':'test'}
You have to save the object in a variable or use it in any way. Just writing in inside a script block does nothing.
The following works fine:
var obj = {'1':'test'};
alert(obj['1']);
I tried in chrome console and firefox console.
Indeed, that doesn't work in Firefox, but, it's normal.
In javascript you can't type JSON without variable declaration before.
So, Firefox doesn't understand it because javascript doesn't understand it.
In fact, I think that Chrome overrides the javascript interpretor to allow declaration without attributions.
Like in python shell when you type 5, it will write 5. So with that Chrome allow you to see the structure of an array or object whatever, just by typings them without declare it into a variable.
You can see an example here :
https://jsfiddle.net/3yqdj599/
let yes = {'1':'test'};
console.log(yes)
// {'1':'test'} => that doesn't work
Finally, don't worry, since a browser is able to execute javascript, if you assign your object in a variable, it will interpret it.
I hope this is clear and helped you ! :)
Cya !

Accessing a JavaScript variable I can see in the console

I am helping to develop a WordPress website. One of the website's plugins "returns" a JavaScript object named FWP. The reason that I put "returns" in quotations is because I'm not clear what returns means. In other words: I'm unclear where FWP is stored.
I can see this object in Chrome's developer console by typing FWP in the console, which then displays the object in the console.
But this all happens in the Chrome developer console. I'm not really doing anything to the variable in the webpage itself. I don't really have access to modify the real FWP object.
Objective: I would like to send this FWP object, as an input parameter, to a custom WordPress plugin that I have written in PHP.
I am confused how to actually grab the FWP object from the webpage, so that I can do things with it.
I know that it's there...somewhere... in the webpage, because I can see it in Chrome's console. Right? I just don't know how to access it.
Is there a general way to access JavaScript variables that one can see in the Chrome developer console? Basically:
1) is there some place where JavaScript variables, visible in Chrome developer console, are stored in the webpage?
2) If so, is there a general method to access them?
Thanks for any guidance. I'm not new to WordPress or php or JavaScript, but I am having a blind spot with this question, and feel that I'm missing something that I haven't learned yet.

GWT client - Uncaught TypeError: Cannot read property 'clear_31_g$' of undefined

I am new to GWT and trying to create a small application. I am currently assembling a small framework for the app, a generic layout handler, etc. This may not be the last problem I will bump into, but I just cannot find any solution to this on google.
So I have a class type, which return me Composites. Also, I have another one, which stores these kind of classes in Stack (I also tried Vector, I thought maybe GWT has issues with it). It didn't matter. If I call the .clear method on the Stack, I have the aforementioned error in the inspection menu of Chrome:
Uncaught TypeError: Cannot read property 'clear_31_g$' of undefined
Like if GWT does not know, how to convert this method to javascript or what? Do you know what is the problem here?
eclipse neon, Java 7 setting on Java SDK 1.8 (maybe this?), GWT 2.7.0 and App Engine 1.9.34
Thanks!
edit1: I also found the page, which contains the emulated JRE classes' list (http://www.gwtproject.org/doc/latest/RefJreEmulation.html) with all supported methods. Now I see, that clear is not on that list for Stack, but empty does and that gives me the same error. :-/
This error simply means that you try to call the clear() method on a null object (the object is undefined).
The error message itself is not as clear as it could be. First, it's not always about reading a property but also about calling a method. Second, remember that you run a compiled to javascript code and the property (or method) name may differ from the original one - it has something like _31_g$ added in the runtime.

Categories