我的JSON电子商务网站的保存数据有问题

时间:2018-07-02 19:13:03

标签: javascript

我似乎找不到问题。当我进入控制台时,它告诉我“ primeProducts”没有定义,即使它显然是定义的。有人能看到我在这里做错了吗?

代码如下所示,

$(document).ready(function(){

    //GLOBAL VARIABLES
    var leftSlideCount = 0;
    var rightSlideCount = 2;
    var slideLeft = "+=100%";
    var slideRight = "-=100%";
    var topSellingSlider = $("#topselling-slider-container");
    var addToBasket = $(".shop-now");
    var productsSlideLeft = $("#top-selling-left-scroll");
    var productsSlideRight = $("#top-selling-right-scroll");
    var basketQty = $("#item-count");
    var basketContent = document.getElementById("basket-content");
    var checkout = document.getElementById("checkoutcompvare");

    function getLocalStorageData(key) {
        return JSON.parse(localStorage.getItem(key));
    }

    function updateLocalStorage(data) {
        localStorage.setItem("basketData", JSON.stringify(data));
    }

    //BASKET DATA (PRODUCTS, QTY etc)
    var basketData = {
        basketQty: 0,
        payForProducts: []
    };

    //SEND BASKETDATA TO LOCALSTORAGE IF IT ISNT ALREADY THERE
    if (localStorage.getItem("basketData") === null) {
        localStorage.setItem("basketData", JSON.stringify(basketData));
    }

    //DISPLAY THE CURRENT NUMBER OF ITEMS IN BASKET FROM LOCAL STORAGE
    if(localStorage && localStorage.getItem('basketData')){
    var getItem = getLocalStorageData("basketData");
    document.getElementById("item-count").innerHTML = getItem.basketQty;
    } else {
    document.getElementById("item-count").innerHTML = 0;
    }

    //Add items to basket page on load//
    function addItemsToBasketPage(currentProduct){
        var getItem = getLocalStorageData("basketData");
        var ulEl = document.createElement("ul");
        basketContent.insertBefore(ulEl, checkout);
        var picLi = document.createElement("li");
        var img = document.createElement("IMG");
        img.setAttribute("src",primeProducts[currentProduct].img);
        picLi.appendChild(img);
        ulEl.appendChild(picLi);
        var firstLi = document.createElement("li");
        firstLi.innerText = topProtein[currentProduct].name;
        ulEl.appendChild(firstLi);
        var secondLi = document.createElement("li");
        var input = document.createElement("input");
        input.setAttribute("class","qtyproduct");
        input.setAttribute("id",currentProduct+"Input");
        input.setAttribute("name","nameQty")
        input.setAttribute("type","number");
        input.setAttribute("min","0");
        input.setAttribute("value",primeProducts[currentProduct].qty);
        secondLi.appendChild(input);
        ulEl.appendChild(secondLi);
        var thirdLi = document.createElement("li");
        thirdLi.setAttribute("id",currentProduct+"listElement");
        thirdLi.innerHTML = "£" + topProtein[currentProduct].price;
        ulEl.appendChild(thirdLi);
    };

    //change subtotal price on qty value change
    $("#basket-content").on("change", 'input[name="nameQty"]', function(){
        var currentQty = this.id;
        var currentVal = this.value;
        var reg = /\w{7}\d+/g;
        var product = currentQty.match(reg);
        product = product.toString();
        var currentPrice = currentVal * topProtein[product].price;
        document.getElementById(currentQty).parentNode.nextSibling.innerText = "£" + currentPrice.toFixed(2);
    })

    addToBasket.click(function(){
        var getItem = getLocalStorageData("basketData");
        getItem.basketQty++;
        var currentProduct = this.id;
        if (!getItem.payForProducts.includes(currentProduct)){
            getItem.payForProducts.push(currentProduct);
            primeProducts[currentProduct].qty = 1;
        } else {
            primeProducts[currentProduct].qty++;        
        }
        updateLocalStorage(getItem);
        document.getElementById("item-count").innerHTML = getItem.basketQty;
    })

    //SLIDE TOP SELLING PRODUCTS LEFT CLICK EVENT
    $(productsSlideLeft).click(function(){
        if (leftSlideCount === 0){
            return;
        }   leftSlideCount--;
            rightSlideCount++;
            $(topSellingSlider).animate({
            left: slideLeft
        }, 900, function() {})

    })

    //SLIDE TOP SELLING PRODUCTS RIGHT CLICK EVENT
    $(productsSlideRight).click(function(){
        if (rightSlideCount === 0){
            return;
        }   leftSlideCount++;
            rightSlideCount--;
            $(topSellingSlider).animate({
            left: slideRight
        }, 900, function() {})

    })

    //******************************** PRODUCTS IN JSON FORMAT ********************************************************//
    var primeProducts = {

        product1: {
            name: "100% WHEY PROTEIN",
            price: 13.99,
            category: "Protein",
            img: "images/index/protein1.jpg"
        },

        product2: {
            name: "MCT COCUNUT OIL",
            price: 17.99,
            category: "Vitamins & Supplements",
            img: "images/index/weight7.jpg"
        },

        product3: {
            name: "Green Coffee Bean Pills",
            price: 14.99,
            category: "Weight Management",
            img: "images/index/weight11.jpg"
        },

        product4: {
            name: "gluten free brown pasta",
            price: 3.99,
            category: "Freefrom",
            img: "images/freefrom/5.jpg"
        },

        product5: {
            name: "CARNEBOLIC PROTEIN",
            price: 22.99,
            category: "Protein",
            img: "images/index/protein6.jpg"
        },

        product6: {
            name: "COMBAT 100% WHEY",
            price: 15.99,
            category: "Protein",
            img: "images/index/protein4.jpg"
        },

        product7: {
            name: "HCE WEIGHT LOSS",
            price: 6.99,
            category: "Weight Management",
            img: "images/index/weight14.jpg"
        },

        product8: {
            name: "DAA Tabvars",
            price: 4.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/daa_tabvars.jpg"
        },

        product9: {
            name: "Tone It Up Plant Protein",
            price: 8.99,
            category: "Protein",
            img: "images/index/protein9.jpg"
        },

        product10: {
            name: "FREE RANGE CHICKEN BROTH",
            price: 1.49,
            category: "Freefrom",
            img: "images/freefrom/2.jpg"
        },

        product11: {
            name: "CLIF BUILDERS PROTEIN",
            price: 11.99,
            category: "Protein",
            img: "images/index/protein5.jpg"
        },

        product12: {
            name: "GL3-750 L-Glutamine",
            price: 16.99,
            category: "Vitamins & Supplements",
            img: "images/sports/11.jpg"
        },

        product13: {
            name: "15 x 43g Detour Caramel Protein Bars",
            price: 8.99,
            category: "Protein",
            img: "images/index/dow/1.png"
        },

        product14: {
            name: "Thermogenic Muscle Preserving Fat Burner",
            price: 6.99,
            category: "Weight Management",
            img: "images/index/dow/2.jpg"
        },

        product15: {
            name: "Advanced Nutrition Lean Pro Diet Protein Shake Vanilla 900g",
            price: 11.99,
            category: "Protein",
            img: "images/index/dow/3.jpg"
        },

        product16: {
            name: "NITROTECH RIPPED CHOLATE FUDGE BROWNIE",
            price: 9.99,
            category: "Protein",
            img: "images/index/dow/4.jpg"
        },

        product17: {
            name: "Free & Easy Dairy Free Cheese Flavour Sauce Mix",
            price: 1.29,
            category: "Freefrom",
            img: "images/index/dow/5.jpg"
        },

        product18: {
            name: "Machine Whey Protein 5lb Accelerated Amino Uptake",
            price: 14.99,
            category: "Protein",
            img: "images/index/dow/6.jpg"
        },

        product19: {
            name: "ADVANCED VISION FORMULA WITH EYE HERBS",
            price: 3.99,
            category: "Vitamins & Supplements",
            img: "images/index/dow/7.jpg"
        },

        product20: {
            name: "Fiber Choice SF Strawberry Weight Mangement",
            price: 5.99,
            category: "Weight Management",
            img: "images/index/dow/8.jpg"
        },

            product21: {
            name: "ALPHA MEN MULTI-VITAMIN FORMULA",
            price: 6.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/alpha_men.jpg"
        },
            product22: {
            name: "Life Extension Glutathione, Cysteine & Vitamin C",
            price: 14.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/amino-acids.jpg"
        },  
            product23: {
            name: "Fusion supplements: DAA",
            price: 8.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/daa_tabvars.jpg"
        },  
            product24: {
            name: "Vitafusion Gorgeous Hair, Skin & Nail Multivitamins",
            price: 13.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/hair_skin_nails.png"
        },  
            product25: {
            name: "Perricone MD Skin Booster Supplements (30 Day Supply)",
            price: 12.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/perricone.jpg"
        },  
            product26: {
            name: "ProCare Health Bariatric Multivitamin",
            price: 9.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/procare.jpg"
        },  
            product27: {
            name: "NutriFlair Thyroid Support Complex Supplement",
            price: 13.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/thyroid-support.jpg"
        },  
            product28: {
            name: "MegaFood Women's One Daily",
            price: 13.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/womans-onedaily.jpg"
        },  
            product29: {
            name: "Liquid Liver Detox (Gluten Free)",
            price: 4.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/8.jpg"
        },  
            product30: {
            name: "Herbel Turmeric 90 Tabvars",
            price: 7.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/9.png"
        },  
            product31: {
            name: "Ginkgo biloba 120mg 350 tabvars",
            price: 14.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/10.jpg"
        },  
            product32: {
            name: "Angiostop Sea Cucumber Extract",
            price: 17.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/11.jpg"
        },  
            product33: {
            name: "Inessa Advanced Daily Multivitamin",
            price: 21.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/12.jpg"
        },  
            product34: {
            name: "RAINBOW LIGHT - Iron-Free Just Once Multivitamin",
            price: 23.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/13.jpg"
        },  
            product35: {
            name: "Prenatal Multivitamin Tabvars",
            price: 14.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/14.png"
        },  
            product36: {
            name: "Angiostop Sea Cucumber Extract",
            price: 9.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/15.jpg"
        },  
            product37: {
            name: "Premtis For Women",
            price: 10.50,
            category: "Vitamins & Supplements",
            img: "images/multivits/16.jpg"
        },  
            product38: {
            name: "Aged Garlic",
            price: 13.49,
            category: "Vitamins & Supplements",
            img: "images/multivits/17.png"
        },  
            product39: {
            name: "Nature's Way Alive! Men's 50+ Gummy Multi-Vitamin",
            price: 12.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/18.jpg"
        },  
            product40: {
            name: "IMMUNE SUPPORT with Vitamin D-3 5,000 & 2oz Grapefruit",
            price: 9.99,
            category: "Vitamins & Supplements",
            img: "images/multivits/19.png"
        }

    };

});

我正在使用崇高的文字。当我单击每个代码的右括号时,它显示了我的起始括号。但是,当我单击右括号时,对象primeProducts不会突出显示第一个括号。我对这里发生的事情感到困惑。

非常感谢您的帮助。

0 个答案:

没有答案