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))
}
Related
I fetch Data and want to output the data. It Works but I also want to make a error handling.
Like:
const func = async data => {
try {
const res = await fetch('MY_URL);
const out = await res.json();
if(out) {
const name = out.result.name;
if(out.result.error) {
console.log('ERROR');
}
}
} catch(e) {
console.log(e);
}
};
Why I get this message?
Cannot read property 'error' of undefined
Ok, I dont have an object with error, but I want to say If there is an error object, then I want to show anything else.
How I fix it?
EDIT:
// Error Handling Statuscode
if (out.result[0].statuscode.error) {
allCircle[0].innerHTML = errorIcon;
domainInfo[0].innerText = out.result[0].statuscode.error;
}
I dont have statuscode.error. But If an Error in my system happens, then I get the property error. So how can I say If error exists? Because error property doesnt always exists.
You could try something like:
if(out.result && out.result.error) {
console.log('ERROR');
}
That way you would shortcut the evaluation of the error property if there is no result property in the first place.
Why I get this message?
Cannot read property 'error' of undefined
this because your out object does not have result parameter every time, and returns undefined at that time so you cant access to out.result.error because out.result is undefined
Solution
if(out?.result?.error) {
console.log('ERROR');
}
I'm new to protractor using javascript, I created a simple script that calls data from another js file.
I have a file called jsobjectdemo.js that contains
<pre>
this.productnames = element.all(by.tagName("app-card"));
this.cartblock = element(by.css("h4 a"));
this.addcartbutton = element(by.css("button[class='btn btn-info']"));
</pre>
And another file called jscalldemo.js
var cart = require('./jsobjectdemo.js');
function addCart(productname){
cart.productnames.each(function(item){
cart.cartblock.getText().then(function(text){
if (text==productname){
cart.addcartbutton.click();
}
})
})
}
When i ran the script, it returns an error
Message:
Failed: Cannot read property 'cartblock' of undefined
Stack:
TypeError: Cannot read property 'cartblock' of undefined
at D:\Trainings\Protractor\protractor-workspace\Protractor\jscalldemo.js:8:14
Const loc = {
productnames = element.all(by.tagName("app-card")), cartblock = element(by.css("h4 a")), addcartbutton = element(by.css("button[class='btn btn-info']"))
}
exports.cart = loc;
Change your jsobjectdemo.js to the above.
And in your jscalldemo.js
Const cart = require('./jsobjectdemo.js').cart;
Now you can use cart.cartblock in jscalldemo.js
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.
I'm new with ReactJS and today I have encountered a few problems.
I am currently using Redux to store my data and I was able to retrieve all the data from the props.
Ie.
const { recipe, loadingRDetail } = this.props;
console.log(recipe.macros);
recipe macros will show me 5 values in array.
Array Image Console Log
But when I tried to accessed to the array, It will throw me an error "Cannot read property '0' of undefined".
I have tried
console.log(recipe.macros[0])
and
const {macros} = recipe;
macros.map((i) => {
....
}...
I have no luck with both of these
This is the error I get
Red Warning error
Actually, it's just because your macros data is asynchronously loaded so you have to add a test to check if it's loaded.
You can try this:
const {macros} = recipe;
if (macros && macros.length) {
macros.map((i) => {
....
}...
}
Or if you already are in your Render method you can just try this :
const {macros} = recipe;
return (
{
macros && macros.length && /* It will check if macros has elements inside */
macros.map((i) => {
....
}...
}
}
)
I have discovered 'localStorage' with this post recently and I am using it to keep an user logged in even if he refresh the web page.
when I only had to save the name of the user, I had no errors, but then, I added a 'grd' variable to save and I got this error:
[Vue warn]: Error in created hook: "ReferenceError: grd is not defined"
This is how I fetch the data when the page loads :
var name = localStorage.getItem(name);
var grade = localStorage.getItem(grd);
if (name !== null){
this.user = name;
}
if (grade !== null){
this.userGrade = grade;
}
and this is how I save the data :
localStorage.setItem(name, '');
localStorage.setItem(grd, '');
What should I change/add to clear the Reference error?
I am using vue.js in this project
You can resolve the reference error by adding quotes to the variables grd and name like so:
var name = localStorage.getItem('name');
var grade = localStorage.getItem('grd');
The referenceces are key values and must be passed as a string.