2 years ago

#65943

test-img

Ctac

Express js: items is not defined, cannot render results from GET route

I am new to express and i am working on a simple application that displays some database items in the view. I wrote a simple route that should render the selected items from the database. The route is the following :

      //get items in basket shopRoute.js
        router.get("/", (req,res)=>{
         let select = "SELECT artikel_name AS name, preis FROM warenkorb_temp"
         dbConn.query(select, (err,result)=>{
             if(err) throw err;
            //console.log(result)
            res.render('/index', {items: result})
        });
});

the templating code is here:

//index.ejs
    <% items.forEach(function(select) { %>
              <div class = wrnkrb-artikels>
                <h3><%= select.name %></h3>
                <h3><%= select.preis %></h3>
            </div>
          <%  }); %>

i get constantly the error, that "items" is not defined when i try to render the solution in the index.ejs. but if I run the template loop in a new file , the results will be rendered.I also want to mention, that i have one GET method already in use in the same rout file, which functions fine on the index.ejs.

javascript

node.js

express

routes

templating

0 Answers

Your Answer

Accepted video resources