JavaScript Cannot read property 'id' of null - javascript

here is my code below..
onRowSelection(event, selectedRule) {
if(event.node && this.props.selectedRule.id !== null){
if ( event.node.selected && !this.props.selectedRule ||
event.node.data.id !== this.props.selectedRule.id) {
this.props.getRule(event.node.data.id);
this.setSelectedRow(event.node.data.id)
}
}
}
I'm receiving an error when i select the row.
Uncaught TypeError: Cannot read property 'id' of null

try following
onRowSelection(event, selectedRule) {
// add checking whether this.props.selectedRule is null or not
if(event.node && this.props.selectedRule !== null && this.props.selectedRule.id !== null){
if ( event.node.selected && !this.props.selectedRule ||
event.node.data.id !== this.props.selectedRule.id) {
this.props.getRule(event.node.data.id);
this.setSelectedRow(event.node.data.id)
}
}
}
Even though this will bypass your error, this wont solve your problem, You need to check why this.props.selectedRule coming null at this point.

onRowSelection(event, selectedRule)
selectedRule isn't used as it is within the body of your function.
My suggestion (based on the few line code you posted) is to use selectedRule where you're using this.props.selectedRule

Thanks
i write the if condition like this
onRowSelection(event) {
if(event.node && event.node.data){
if ( event.node.selected && !this.props.selectedRule){
this.props.getRule(event.node.data.id);
this.setSelectedRow(event.node.data.id)
}
if(this.props.selectedRule){
if ( event.node.data.id !== this.props.selectedRule.id) {
this.props.getRule(event.node.data.id);
this.setSelectedRow(event.node.data.id)
}
}
}
}
it seems working now.

Related

typeof body changes from string to undefined when placed in a conditional

The last if block does not take in the value of the body variable and converts it to undefined. The console log before the block shows the type of the variable is "string" but somehow in the condition and statement of the conditional the body turns to undefined.
doc = doc.map(el => {
let { title, body, _id, likes } = el
if (title && title.length > 20) {
el = {
...el,
title:`${title.slice(0, 20)}...`,
}
} else if (!title || typeof title === 'undefined') {
el = {
...el,
title: '',
}
}
console.log(typeof body)
if (body.length > 55) {
el = {
...el,
body: `${body.slice(0, 55)}...`,
}
}
title = el.title
body = el.body
console.log(typeof body)
return {_id,title,body,likes}
})
The terminal output of the logs is as follows:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
App running on port 5000...
string
undefined
string
undefined
string
undefined
The three different results represent three different documents in the doc array and hence the six console log outputs pertaining to each body value and its change.
I do not use semicolons in javascript, could it be the cause of this problem?
Still no idea why the earlier code didn't work but this much smaller code seems to do the trick
doc = doc.map(el => {
let { title, body, _id, likes } = el
body = `${body.slice(0, 55)}...`
if (title && title.length > 20) {
title=`${title.slice(0, 20)}...`
} else if (!title || typeof title === 'undefined') {
title= '...'
}
return {_id,title,body,likes}
})
It works because of the fact that I don't need to check if the length of body is greater than 55, since a smaller value will always restore to its original size. So I trim the body first and then the title.
Thanks to anyone who helped! Will still like to find the error/ problem with earlier code.

Trying to figure out the issue with my code

I have a problem with my code and try to find the error
I have already tried to use let instead of var but it still did not work. There is also no output in the console.
I believe the error to be within the if (tylerdel == true) of my code:
if (command === prefixfile.prefix + `active`) {
var tylerdel = true
message.channel.send (`test`)
if (tylerdel == true) {
if (message.author.id === ("")) {
message.delete (1)
}
}
}
It is supposed to delete a message if it comes from a certain person but I also need it to be toggleable.
As per your code boolean variable tylerdel will always be true so there is no need to use this variable in your if condition.
if(command === prefixfile.prefix + 'active') {
message.channel.send('test');
if (message.author.id === '') {
message.delete(1);
}
}
Be careful with == (Equality) and === (Identity).
More info about the operators
You should know what is "Debugging". You can try printing something in each if to see where is the problem.
Hope this helps you to solve your problem.
Try this:
if (command === prefixfile.prefix + 'active') {
var tylerdel = true;
message.channel.send ('test');
if (tylerdel) {
if (message.author.id === '') {
message.delete(1);
}
}
}

Checking for null in Javascript but still receiving TypeError

When I run this code, I receive this error:
TypeError: Cannot read property 'outerHTML' of null
Here is my code:
let image = document.querySelector('.gallery__large-image-link');
if (image !== null || image !== '' || typeof(image) !== 'undefined') {
var html = image.outerHTML;
//do stuff
}
Since you're using ||, if any of the conditions are true, the block executes - and given the !== tests, the block will always execute. Use && instead. Also, correct your typeof syntax:
const image = document.querySelector('.gallery__large-image-link');
if (image !== null && image !== '' && typeof image !== 'undefined') {
const html = image.outerHTML;
//do stuff
}
Or simply check to see if image is truthy:
const image = document.querySelector('.gallery__large-image-link');
if (image) {
const html = image.outerHTML;
//do stuff
}
You can just use this code
let image = document.querySelector('.gallery__large-image-link');
if (image) { // this will check for undefined and null
var html = image.outerHTML;
//do stuff
}

sessionStorage returns undefined in one field, but works in other

I have this code:
// Run on page load
window.onload = function() {
// If values are not blank, restore them to the fields
var personal_number = sessionStorage.getItem('personal_number');
if (personal_number !== undefined){
$('#personal_number').val(personal_number);
}
var email = sessionStorage.getItem('email');
if (email !== undefined){
$('#username').val(email);
}
var password= sessionStorage.getItem('password');
if (password !== undefined){
$('#password').val(password);
}
}
// Before refreshing the page, save the form data to sessionStorage
window.onbeforeunload = function() {
sessionStorage.setItem("personal_number", $('#personal_number').val());
sessionStorage.setItem("email", $('#username').val());
sessionStorage.setItem("password", $('#password').val());
}
This "works" if I add a value to personal_number, email and password it gets saved to my sessionStorage and when I refresh the page, my input fields get rightly populated.
But when I go to another page (within the same website) and return back, I get undefined outputed in personal_number input field and the correct values in the others.
I dont understand why!
First, it should not output anything if the value is undefined because: if (personal_number !== undefined).
Second, how come only personal_number is not working and the two other fields are fine?
Update:
I removed password field since cacheing it is insecure.
I have also figured out what is causing the problem, but I have not solved it.
When I visit a page with no input fiels and return then, now, all my fields output undefined. So it seems like i get a new session when I do so.
But how come it still ouputs undefined?
(function ($) {
// Run on page load
window.onload = function() {
var person_number = sessionStorage.getItem("person_number");
var email = sessionStorage.getItem("email");
if (person_number !== undefined || person_number !== null ||
person_number != undefined || person_number != null){
$('#person_number').val(person_number);
}
if (email !== undefined || email !== null ||
email != undefined || email != null){
$('#username').val(email);
}
}
// Before refreshing the page, save the form data to sessionStorage
window.onbeforeunload = function() {
sessionStorage.setItem("person_number", $('#person_number').val());
sessionStorage.setItem("email", $('#username').val());
}
})(jQuery);
I know I am testing for to much, but there is no way this should output undefined!!
Update 2
I also updated it so it will not save inputs if the input is undefined/null. But it still does not work..
(function ($) {
// Run on page load
window.onload = function() {
var person_number = sessionStorage.getItem("person_number");
var email = sessionStorage.getItem("email");
if (person_number !== undefined || person_number !== null ||
person_number != undefined || person_number != null){
$('#person_number').val(person_number);
}
if (email !== undefined || email !== null ||
email != undefined || email != null){
$('#username').val(email);
}
}
// Before refreshing the page, save the form data to sessionStorage
window.onbeforeunload = function() {
var person_number = $('#person_number').val();
var email = $('#username').val();
if (person_number !== undefined || person_number !== null ||
person_number != undefined || person_number != null){
sessionStorage.setItem("person_number", person_number);
}
if (email !== undefined || email !== null ||
email != undefined || email != null){
sessionStorage.setItem("email", email);
}
}
})(jQuery);
Your question is quite old and you might have had your answer, by now, but if not, or for any other who's having the same issue :
I think your jquery code is loaded on every page and thus, your onbeforeunload statement tries to store your fields' value on every page, even where the field does not exist and that's when the Items get "undefined".
Before you try to store the value of a field, make sure the field actually exists, using getElementById, for example, like this :
var fieldExists = document.getElementById('person_number');
if(fieldExists !== null) {
sessionStorage.setItem('person_number', $('#person_number').val());
}
And in your onload, you don't need to test if (person_number !== undefined and so ... only if it's null
if (person_number !== null) {
$('#person_number').val(person_number);
}
Hope this helps
try something like this:
sessionStorage.personal_number
this will be undefined if not set
you can set values using this:
sessionStorage.personal_number = 123456

Error when trying to check typeof undefined json object

When Im trying to check if the [action] is defined I keep getting javascript errors.
if((typeof array_from_php.api_description[mobile_type][action]) != 'undefined') {
console.log('defined');
api = array_from_php.api_description[mobile_type][action];
} else {
console.log('undefined');
mobile_type = 0;
api = array_from_php.api_description[mobile_type][action];
}
Error:
Uncaught TypeError: Cannot read property 'register_mobile' of undefined
you need to check each property like
if(array_from_php && array_from_php.api_description && array_from_php.api_description[mobile_type] && (typeof array_from_php.api_description[mobile_type][action]) != 'undefined') {
console.log('defined');
api = array_from_php.api_description[mobile_type][action];
} else {
console.log('undefined');
mobile_type = 0;
api = array_from_php.api_description[mobile_type][action];
}

Categories