2

I am getting Error while parsing Json LocalStorage variable in PUG/JADE

extends base.pug

block style
    style
        include ../static/trendbar.css

block scripts
    //- script(src="../static/jquery.js")
    //- script(src="../static/jquery2.js")

block content
    script!= "var items = []; items =JSON.parse(localStorage.getItem('obj')); console.log(items);"     
    .product
        h3.trendbar
            | Trending
        .row#loadProduct
            - var items1 = items;
            each n,i in items1
                .column
                    a.itemcon#cursor(href="/p/"+n.PID )
                        .pitem
                            .pitemMage
                                - var img = "../static/hookah/" + n.PID + ".jpg"
                                img(src= img alt ='NO MORE PRODUCT')
                            .pitemAvail
                                |   #{n.availability}
                            .pitemCost
                                p.prod_name
                                    |  #{n.name}
                                .prod_price
                                    p.realP
                                        |  ₹#{n.Mprice}
                                    p.ourP
                                        |  ₹#{n.Oprice}

TypeError: F:\hookahboi\views\trending.pug:18 16| .row#loadProduct

17|             - var items1 = items;

18| each n,i in items1

19|                 .column

20|                     a.itemcon#cursor(href="/p/"+n.PID )

21|                         .pitem

Cannot read property 'length' of undefined at eval (eval at wrap (F:\hookahboi\node_modules\pug-runtime\wrap.js:6:10), :188:32) at eval (eval at wrap (F:\hookahboi\node_modules\pug-runtime\wrap.js:6:10), :279:4) at template (eval at wrap (F:\hookahboi\node_modules\pug-runtime\wrap.js:6:10), :340:7)

6
  • Pug templates are processed server-side. LocalStorage is client-side, in the browser. These are two completely different things, on two completely separate machines, that execute at different times. There is no way Pug can simply access the localStorage. Please see the difference between server-side and client-side programming. Commented Nov 28, 2020 at 12:56
  • so is there any way to store and display localstorage data, I am using Pug,Nodejs and I really dont know that pug is ServerSide. Commented Nov 28, 2020 at 13:03
  • 2
    Imagine you write a letter. The letter says, "Dear sir, please write here the names of your children : ______________". Then you fold the letter, send the letter by post mail. Then the person receives the letter and then writes the names of their children on the paper. See? There is NO WAY you could read the names of the children before the person writes them three days later. This is exactly what you are trying to do now with Pug. You are trying to display data that will exist later in time, in a different house, written by a different person with a different pen. Commented Nov 28, 2020 at 13:08
  • 2
    i already save some json data to LocalStorage. if i knew That Pug is server Side Then i dont waste so much time on it. Thanks for Saving my time , now i will learn something new technique that i could use to solve this issue. thanks once again Commented Nov 28, 2020 at 13:16
  • 1
    You don't know that you are writing server-side code? So you don't know if you are the person writing the letter or the person receiving the letter? :) Anyway you're welcome, programming is such a vast and complicated matter. Commented Nov 28, 2020 at 13:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.