简体   繁体   English

SyntaxError:JSON解析错误:意外的标识符“功能”

[英]SyntaxError: JSON Parse error: Unexpected identifier “function”

First time posting. 第一次发布。 I have been looking all over for an answer but can seem to find any working solution. 我一直在寻找答案,但似乎可以找到任何可行的解决方案。 I am debugging a shopping cart I built in ES6. 我正在调试我在ES6中构建的购物车。 It seems to work flawlessly on Chrome but on Safari and Firefox I have been getting an error. 它似乎可以在Chrome上完美运行,但是在Safari和Firefox上却出现错误。

Error in Safari: Safari中的错误:

SyntaxError: JSON Parse error: Unexpected identifier "function"

Error in Firefox: Firefox中的错误:

SyntaxError: JSON.parse: unexpected keyword at line 1 column 1 of the JSON data[Learn More]

My code: 我的代码:

export default class productUtil{
    constructor (){
    }

    addToCart (sku, price){
        let product={price, quantity:1};
        if (sessionStorage.getItem(sku) == undefined){
            sessionStorage.setItem(sku, JSON.stringify(product));
        }
        else {
            let oldValue=JSON.parse(sessionStorage.getItem(sku, product)); 
            let newValue = oldValue.quantity + 1;
            product.quantity = newValue;
            sessionStorage.setItem(sku,JSON.stringify(product));
        }
        this.cartBuilder(sku, product);
    }


    cartBuilder(sku, product){
        document.getElementById('listItems').innerHTML="";
        if (sessionStorage){

            for(let key in sessionStorage){
                let cartItem = document.createElement("div");
                cartItem.setAttribute("id","itemRows");
                product = JSON.parse(sessionStorage[key]);
                let totalPrice = product.price*product.quantity;

                cartItem.innerHTML=(
                        '<div>'+key+'</div>'+
                        '<input class="cart_input_size" id="'+key+'" type="number" value="'+product.quantity+'">'+
                        '<div>'+'$'+product.price+'</div>');
                document.getElementById('listItems').appendChild(cartItem);
            }   
        }
    }
}

Thank you, appreciate any help! 感谢您的帮助!

You are iterating the prototype also. 您也在迭代原型。 Use Object#hasOwnProperty() to check if it is prototype or enumerable property 使用Object#hasOwnProperty()检查它是原型还是可枚举的属性

Try 尝试

for(let key in sessionStorage){
  if(sessionStorage.hasOwnProperty(key)){
    // process storage item
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:JSON解析错误:意外的标识符“对象”(匿名函数) - SyntaxError: JSON Parse error: Unexpected identifier “object” (anonymous function) SyntaxError:JSON解析错误:意外的标识符“object” - SyntaxError: JSON Parse error: Unexpected identifier “object” SyntaxError:JSON解析错误:尝试解析JSON时出现意外的标识符“对象” - SyntaxError: JSON Parse error: Unexpected identifier “object” when trying to parse json JSON 解析错误:意外的标识符“be” - JSON Parse error: Unexpected identifier "be" API 获取问题:未处理的承诺拒绝:语法错误:JSON 解析错误:意外的标识符“服务器” - API Fetch Issue: Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier “Server” JSON 解析错误:意外的标识符“对象” - JSON parse error: Unexpected identifier “object” JSON解析错误:意外的标识符“ Try” - JSON Parse error: Unexpected identifier “Try” "JSON 解析错误:意外的标识符“未定义”" - JSON Parse error: Unexpected identifier "undefined" React Native JSON 解析错误意外的标识符 - React Native JSON Parse error Unexpected identifier JSON 解析错误:意外的标识符“RCTWebSocketModule” - JSON Parse error: Unexpected identifier "RCTWebSocketModule"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM