I want delete image only when storageId is not null.
I checked for null like so and value is null but it's fired so exception says not found that image.
How can I check if it's null?
const data = snapshot.data();
const ref = firebase.storage().ref();
if (data.storageId != null) {
ref.child('images').child(`${data.storageId}.jpg`).delete();
}
null is falsey so
if (data.storageId) {
ref.child('images').child(`${data.storageId}.jpg`).delete();
}
This will also cater for storageId is undefined.
Related
I have that problem: I'm trying to reach the property of on object "user" that I save in localStorage and the browser gives me the error "Cannot read properties of undefined".
I apply my code
like(recipe) {
if (this.favorites.indexOf(recipe) === -1) {
this.favorites.push(recipe)
}
let user = JSON.parse(localStorage.getItem("users"));
let currFav = user.favorites;
currFav.push(recipe);
localStorage.setItem("users", JSON.stringify(user))
}
I am new to Javascript, I am trying to invoke code on onClick event inside React container component below is my code
const onClickHandler = (e) => {
if (e != null){
const row_id = e.target.getAttribute('key');
var cur_row;
if (api_data != undefined || api_data != null){
for (cur_row=0; api_data.length-1; cur_row++){
console.log('this is api data', api_data[cur_row]['id'])
if (api_data[cur_row]['id'] == row_id){
console.log(api_data[cur_row])
break
}
}}
}}
in above code api_data is hookstate variable in which i am storing data. in console log I am able to see the data but when it comes to if condition which is right below the console.log statement I am getting error 'Cannot read property 'id' of undefined'. I searched some similar questions but not able to understand who to fix my problem. Any idea what I am missing
const onClickHandler = (e) => {
if (e != null){
const row_id = e.target.getAttribute('key');
var cur_row;
if (api_data != undefined || api_data != null){
/*
I guess you forgot to add exit statement in for loop,
because of it curr_row value was increasing way beyond
api_data.length giving you undefined output and hence that error
*/
for (cur_row=0; curr_row <= api_data.length-1; cur_row++){
console.log('this is api data', api_data[cur_row]['id'])
if (api_data[cur_row]['id'] == row_id){
console.log(api_data[cur_row])
break
}
}}
}}
I try to resolve includes undefined, For that, I am using && operator.
isAllChecked = label => {
const { permission } = this.state;
false value - I need false value when I get data from API,
But I got an error that includes undefined. for that, I used && operator
but I got a true value, I don't need to get true value
const data = !groupItems.some(
x => !permission && !permission[label] && !permission[label].includes(x)
);
// console.log(true) //true
const data = !groupItems.some(
x => !permission[label].includes(x)
);
// I got a false value using static data without using && operator
// data output: false (accepted output- getting using static data, but I need
to get the same value when I get data from API, I got an error includes undefined without using && operator)
return data;
};
However, If I got data from API, this method is displayed can not read property undefined and when I am going to resolve 'includes' of undefined, I used && operator and I got true value.
Cannot read property 'includes' of undefined.
I don't need true value, I need false value for the initially mounted component.
my question is that how can I resolve Cannot read property 'includes' of undefined by using && operator or anything.
Here is my code sandbox: https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k
Instead of some, you can use every to check all.
isAllChecked = label => {
const { permission } = this.state;
const data = groupItems.every(
x => permission && permission[label] && permission[label].includes(x)
);
return data;
};
I am storing particular key value in Database. But, While fetching the key value, getting undefined error.
await DbHandler.fetch(codeStatus)
.then((result) => {
const codeEnabledObj = result[0];
console.log('codeEnabledObj', codeEnabledObj);
let codeEnabled = false;
if (codeEnabledObj && codeEnabledObj.length > 0) { // this code not executing at all.
codeEnabled = codeEnabledObj[0].isEnabled;
}
console.log('codeEnabled', codeEnabled); // getting false always
console.log('codeEnabledObj.length[0]', codeEnabledObj.length); // undefined
})
.catch((error) => {
});
The issue is, It's not going inside if condition and throwing error like undefined.
But, If we print the response from db fetch
'codeEnabledObj', { type: 'codeStatus', isEnabled: true } // This is my response
Any suggestions?
Objects don't have length property like an array.
codeEnabledObj.length is wrong
use this,
Object.keys(codeEnabledObj).length
EDIT :
codeEnabledObj[0].isEnabled should be only codeEnabledObj.isEnabled
There is no property length in the codeEnabledObj , Moreover its not an Array.. so modifying the condition would work, where isEmpty could be a function used from
node package as underscore
if (isEmpty(codeEnabledObj)) { // ... }
and
codeEnabledObj.isEnabled
Thanks.
I have go through the other solution which are available in stackoverflow bt it didnt work for me.So please someone suggest me what to do in my case.
In my case when i am performing delete operation its giving error on console,bt remember its not reflecting on deleting operation,its working fine.I just want to remove or solve this error.
Here is my console error:
TypeError: Cannot read property 'concat' of undefined
at ChildScope.$scope.deleteJob (uploadController.js:454)
at $parseFunctionCall (angular.js:12474)
Here is my code at line no. 454:
//delete JD
$scope.deleteJob = function (fileName) {
$scope.loadingJobs = true;
_deleteFile(fileName, AppConstants.docType.JOBDESCRIPTION);
//line no.454
if ((jdService.getjdFileName() === fileName) || ((jdService.getjdFileName().concat(".xml") === fileName))) { //line no.454
$scope.isjdDeleted = false;
jdService.setisSelected(false);
}
};
Here is my method where i am getting jdName from UI :
jdService.setjdFileName(fileName);
and here is my service where i have defined my jdFileName methods:
var jdService = [
function() {
var jdFileName;
return {
getjdFileName: function () {
return jdFileName;
},
setjdFileName: function(value) {
jdFileName = value;
},
Please suggest me what to do in that,Thanks
If you change the expression in if to:
if ((jdService.getjdFileName() === fileName) || (((jdService.getjdFileName() || '' ).concat(".xml") === fileName)))
then this part (jdService.getjdFileName() || '' ) will evaluate either to the value that is returned by jdService.getjdFileName() or, if that value is falsy, than to the empty string (''). This way you can ensure that concat() will always be called on a string.