Undefined variable error for already defined variable - javascript

Variable bodydata is already declared in function myfunction.This variable is then passed to another function createPDFTable but in createPDFTable function....it is giving an error as :
"uncaught TypeError: Cannot read property 'push' of undefined".......AT line 129
Variable bodydata which is being passed to function createPDFTable is treated as undefined.Why and how to rectify this?
Fiddle link is -> https://jsfiddle.net/o8L3p0kk/

Related

Can't get DOMelement by using variable in my environment but in console i can

My button in my websites have onclick for function addNewSetPlanningReskilling().
But I CAN'T get DOMelement by using variable in my environment, in console i CAN.
function addNewSetPlanningReskilling(){
numberOfSets++;
console.log(numberOfSets); // here i have number as i want to have, for example 3,4,5..
document.querySelector(`[data-key='${numberOfSets}']`).style.display = 'block';
}
Uncaught TypeError: Cannot read property 'style' of null at addNewSetPlanningReskilling at HTMLButtonElement.onclick
I found solution for this :
change this :
document.querySelector(`[data-key='${numberOfSets}']`)
for this:
document.querySelector("[data-key='"+numberOfSets+"']")

Postman test throws "TypeError: Cannot read property 'get' of undefined"

How do I make it work?
Goal: call a variable inside test script
Test script:
pm.test("shipment registration not allowed", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.results[0].errors.title).to.eql(pm.errors.get("shipmentRegistrationNotAllowed"));
});
The error:
Error: TypeError: Cannot read property 'get' of undefined
I'm not sure where you have found pm.errors.get as a function that you can use but I don't believe it is something that is within Postman.
All the sandbox functions can be found here https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference
If you are just looking to assert that jsonData.results[0].errors.title equals a string value, you could just do this:
pm.test("shipment registration not allowed", () => {
var jsonData = pm.response.json()
pm.expect(jsonData.results[0].errors.title).to.eql(pm.globals.get("my_error_value"))
})
If you set a global variable you can reference in the test like this:

Uncaught TypeError: Cannot read property 'attributes' of undefined

Hello everyone i am trying to perform delete operation in my web app,when i am deleting an element from DB, in console its showing this error.Please give your valuable solution for this Thanks in advance.
Uncaught TypeError: Cannot read property 'attributes' of undefined
at _clearTreeSelection (orphanController.js:888)
at Scope.$scope.clearSelection (orphanController.js:659)
at HTMLDivElement.<anonymous> (orphanController.js:1058)
at line number 888 this is my code:
//This is to clear the selectedNode of angular tree on modal close
_clearTreeSelection = function () {
$scope.orphanData.orphanText =
$scope.orphanData.orphan.attributes.text;//This is line no.888
if ($scope.OntologyTree && $scope.OntologyTree.currentNode) {
$scope.OntologyTree.currentNode = null;
}
};
at line number 659 this is my code:
$scope.clearSelection = function () {
_clearTreeSelection();//line no.659
_clearGazetteerSelection();
};
at line number 1058 this is my code:
_modal.on('hidden.bs.modal', function () {
$scope.suggestionListSearchText = '';
$scope.clearSelection();// line no. 1058
});
}
I have checked all over stackoverflow, i cant find the suitable solution for this problem.
This means that your $scope.orphanData.orphan is undefined. Find where have you declared this object and be sure that it refers to an object.
You are getting this error since you are trying to access a property on a object but that object does not exist. You should first make sure that orphan property actually exists as an object so that you can access attributes property on it.
Changes you need to make:
$scope.orphanData.orphan = {'attributes': {}}

Trace undefined variable in JavaScript

I'm getting the following error:
libs.js:3392 TypeError: Cannot read property 'offsetHeight' of undefined
at Object.b.updateAutoHeight (http://localhost:3000/.../scripts/libs.js:1529:12362)
at Object.b.slideTo (http://localhost:3000/.../scripts/libs.js:1530:1740)
at Object.b.init (http://localhost:3000/.../scripts/libs.js:1530:25098)
at Object.t (http://localhost:3000/.../scripts/libs.js:1530:27249)
at HTMLDivElement.<anonymous> (http://localhost:3000/.../scripts/libs.js:1529:107)
at Function.each (http://localhost:3000/.../scripts/libs.js:1409:2882)
at n.each (http://localhost:3000/.../scripts/libs.js:1409:847)
at n.e.fn.swiper (http://localhost:3000/.../scripts/libs.js:1529:85)
at Carousel.activate (http://localhost:3000/.../scripts/app.js:1802:34)
at Carousel.toggleActivation (http://localhost:3000/.../scripts/app.js:1824:57)
Now I know the error is that some variable is being passed into a library, that is undefined when it isn't supposed to be. How do I backtrace the variable that is being passed that is undefined?

Uncaught TypeError: Cannot read property 'focus' of undefined

This issue is occurring when my page loads.
Using the following scripts
-jquery.simplemodal-1.4.3.js
-jquery v1.7.1
Below is a small code snap, of the code inside simplemodal where this error is occuring.
focus: function (pos) {
var s = this, p = pos && $.inArray(pos, ['first', 'last']) !== -1 ? pos : 'first';
// focus on dialog or the first visible/enabled input element
var input = $(':input:enabled:visible:' + p, s.d.wrap);
setTimeout(function () {
input.length > 0 ? input.focus() : s.d.wrap.focus();
}, 10);
},
Any Ideas that i could do to resolve this would be great
This is an old question and the original poster has probably resolved his concrete issue.
But the general questions related to the Error Messages:
Uncaught TypeError: Cannot read property '...' of undefined
Uncaught TypeError: Cannot read property '...' of null
and
Uncaught TypeError: Cannot set property '...' of undefined
Uncaught TypeError: Cannot set property '...' of null
are still important for many peoples and have a simple and general answer:
The object of that we try to read property, set property or call a function
- is not declared or
- is declared, but not defined or
- is null
Simple test code in Google Chrome
<script type="text/javascript">
// object obj is not declared, there is no previos 'var obj;'
obj.prop; // Uncaught TypeError: Cannot read property 'prop' of undefined
obj.prop = "value1"; // Uncaught TypeError: Cannot set property 'prop' of undefined
obj.func(); // Uncaught TypeError: Cannot read property 'func' of undefined
// ------------------------------------------------------------------------
// object 'obj' is declared with:
var obj;
// but it is not defined, there is no value assigned to it (obj = 5 or something else)
obj.prop; // Uncaught TypeError: Cannot read property 'prop' of undefined
obj.prop = "value1"; // Uncaught TypeError: Cannot set property 'prop' of undefined
obj.func(); // Uncaught TypeError: Cannot read property 'func' of undefined
// ------------------------------------------------------------------------
// object 'obj' is declared and defined. Value is null
var obj = null;
obj.prop; // Uncaught TypeError: Cannot read property 'prop' of null
obj.prop = "value1"; // Uncaught TypeError: Cannot set property 'prop' of null
obj.func(); // Uncaught TypeError: Cannot read property 'func' of null
// ------------------------------------------------------------------------
// object 'obj' is declared and defined
var obj = {prop: "propertyValue", func: function() {return "returnValue"}}
// there are no errors
</script>
The same code in Firefox:
// object obj is not declared, there is no previos 'var obj;'
obj.prop; // Uncaught TypeError: obj is undefined
obj.prop = "value1"; // Uncaught TypeError: obj is undefined
obj.func(); // Uncaught TypeError: obj is undefined
-----------------------------------
// object 'obj' is declared with:
var obj;
// but it is not defined, there is no value assigned to it (obj = 5 or something else)
obj.prop; // Uncaught TypeError: obj is undefined
obj.prop = "value1"; // Uncaught TypeError: obj is undefined
obj.func(); // Uncaught TypeError: obj is undefined
-----------------------------------
// object 'obj' is declared and defined. Value is null
var obj = null;
obj.prop; // Uncaught TypeError: obj is null
obj.prop = "value1"; // Uncaught TypeError: obj is null
obj.func(); // Uncaught TypeError: obj is null
-----------------------------------
// object 'obj' is declared and defined
var obj = {prop: "propertyValue", func: function() {return "returnValue"}}
// there are no errors
So there are some differences in the Error Message between browser, but the massage is clear:
The object in not defined or null.
In the general case it is easy to understand what exactly the error message means.
But why this happens in concrete cases?
The reasons could by many, but the important ones could be:
Calling a function or accessing a property
before it is initialized/defined
These could happen, if an object (popup, modal), created on the fly, is not ready yet.
after it is destroyed
Structure changes by using new versions of an API, where a property or function was
renamed or
moved to other objects
By using loops on arrays: the index don't exist, because of
bad incrementation/decrementation or
bad condition
How to debug?
Find on which object and where the error is thrown
Read the documentation to assure that,
try using the object on the right place,
have called all needed functions, before using the object
Look in the scope/function if the object is defined,
If not, look in the stacktrace in its caller(s)...

Categories