繁体   English   中英

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

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

第一次发布。 我一直在寻找答案,但似乎可以找到任何可行的解决方案。 我正在调试我在ES6中构建的购物车。 它似乎可以在Chrome上完美运行,但是在Safari和Firefox上却出现错误。

Safari中的错误:

SyntaxError: JSON Parse error: Unexpected identifier "function"

Firefox中的错误:

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

我的代码:

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);
            }   
        }
    }
}

感谢您的帮助!

您也在迭代原型。 使用Object#hasOwnProperty()检查它是原型还是可枚举的属性

尝试

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM