I am working on creating this website and my Javascript is having issues.
I'm getting the error: Uncaught SyntaxError: Unexpected identifier
This occurs with one of my lines on my Javascript.
var temp=localStorage.getItem(volunteerOpp);
newObject = JSON.parse(temp);
display newObject[0];
It is showing this error on line 3, I think and I'm unsure what the problem is.
That's the error JSON.parse throws when you don't feed it nice json. Have a close look at volunteerOpp for syntax errors, perhaps a property name not in double quotes.
I'm not sure what you want, but you can try this:
var temp = localStorage.getItem(volunteerOpp),
newObject = JSON.parse(temp);
console.log(temp);
console.log(newObject);
console.log(newObject[0]);
Related
I am new to JSON and struggling with an error in a small section of code.
The code is:
this.json = function() {
return this.text().then(json.parse);
};
The error is:
SyntaxError: JSON.parse: unexpected character at line 1 column 3 of the JSON data
Can anyone see what the issue is?
It's not very clear what the JSON you're trying to parse is. Your code suggests that this.text() returns a promise and you want to pipe the text into the JSON.parse function. That all looks ok.
Update with the output of this.text() and we can see what's invalid in your JSON.
I am trying to run a while loop to remove all the elements of a particular class name from the DOM as given in the answer here. For some reason, I am getting an error.
Uncaught SyntaxError: Invalid or unexpected token
The code I am trying to run is lengthy but the basic idea is identical to the above-linked answer.
function resetGame() {
var cardColumns = document.getElementsByClassName('card-column');
while (cardColumns[0]) {
cardColumns[0].parentNode.removeChild(cardColumns[0]);
}
}
When I hover over the error (which shows at the end of the loops curly brace) it shows \u200b in Chrome Inspect.
I have tried changing the class name, and even directly copy-pasting the guy above answer. Same error. When I paste the loop into the console on its own I get the same thing, so my best assumption is I have something incorrect fundamentally with the syntax, not the rest of my code.
Any ideas?
A problem. Again. Here is the code:
if(localStorage.getItem("temporaryArray")){
var temporaryArray = JSON.parse(localStorage.getItem("temporaryArray"));
}else{
var temporaryArray = [];
}
So basically what it does is that when a new page is loaded I don't want to reset the array because later in the code I assign something to this array in localStorage. So what I'm trying to say is that if you can get this item, then when the code is loaded again assign this variable to the localStorage item. Else, just set it to an empty array. But here is the error I'm getting because the array is currently empty(I think that's the reason why):
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at main.js:61
Any help would be very much appreciated.
If a method invocation fails, it usually helps to look at what the input to the function is. I would recommend doing console.log(localStorage.getItem("temporaryArray")) before the JSON.parse() call to see what the problem is.
What I would guess the source of the problem is, is that at some point you are calling localStorage.setItem("temporaryArray",value) and have forgotten to do JSON.stringify() on the value.
I have a problem parsing a string variable back to an object.I have looked at all other question around this topic but none solve my problem.
if(subMatch.match(/\{.*\}/)){ /// new Object of some sort
var objStr=subMatch.match(/\{.*\}/)[0];
//objStr= JSON.stringify(objStr); // I tried this , no difference
//objStr='"'+objStr+'"'; // Tried this way: unexpected token t
//objStr="'"+objStr+"'"; // Tried this way: unexpected token '
objStr=JSON.parse("'"+objStr+"'"); // puts out unexpected token '
This is the string I am trying it out on:
{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}
According to JSONLint it is valid. With the extra quotes it looks like:
'{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}'
I looked at this question,
JSON.Parse,'Uncaught SyntaxError: Unexpected token o
but their variable starts as an object. My objStr has type String, I checked.
I am adding literal single quotes around objStr.
Because objStr is already a String, that should not be a problem right?
I've also tried completely without extra quotes around the variable
How can I correctly JSON.parse a String variable.
I can get it to work with eval, but I rather not use that, because it is user input that I have to put in an object.
Sorry to bother you with a question about this topic again, but I haven't found the solution among the other questions.
Help would be really appreciated!
Thanks
Jenita
As CBroe says, JSON.parse() can parse JSON just fine on its own and whatever you're trying to do here is preventing it from doing that.
It doesn't need your help. Just let it do its job and get rid of all that mess:
var obj = JSON.parse(objStr);
I'm trying to pass a variable that looks like 68679786987698_987687697869786 to a function in Javascipt, but I'm getting the error Uncaught SyntaxError: Unexpected token ILLEGAL in Chrome's developer console. It looks like the underscore is the problem, but I need it to stay there. Any suggestions?
Here's the relevant code:
entry += '<span>Like';
function likePost(id) {
alert('like');
}
Use quotes:
entry += '<span>Like';
post.id must be a string if it contains the underscore.