0

Working on node.js and mongodb app. I am having this issues in node.js apps, which is given below and i also added my index.ejs file and pin.js file:

ReferenceError: /home/Documents/Build-A-Pinterest-Clone/views/pins/index.ejs:6
    4| <div class="container">
    5|   <div id="pins">
 >> 6|    <% for(var i = 0; i < pins.length; i++) { %>
    7| 
    8|   <div class="" style="margin:20px">
    9|   <a href="/pins/details/<%= pins[i]._id %>"><img src="<%= pins[i].path %>" style="width: 200px" /></a>

pins is not defined
    at eval (/home/Documents/Build-A-Pinterest-Clone/views/pins/index.ejs:11:27)
    at index (/home/Documents/Build-A-Pinterest-Clone/node_modules/ejs-mate/node_modules/ejs/lib/ejs.js:682:17)
    at tryHandleCache (/home/falguni/Documents/Build-A-Pinterest-Clone/node_modules/ejs-mate/node_modules/ejs/lib/ejs.js:254:36)
    at Object.exports.renderFile (/home/Documents/Build-A-Pinterest-Clone/node_modules/ejs-mate/node_modules/ejs/lib/ejs.js:485:10)
    at View.renderFile [as engine] (/home/Documents/Build-A-Pinterest-Clone/node_modules/ejs-mate/lib/index.js:227:7)
    at View.render (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home//Documents/Build-A-Pinterest-Clone/node_modules/express/lib/response.js:1012:7)
    at /home//Documents/Build-A-Pinterest-Clone/routes/main.js:9:6
    at Layer.handle [as handle_request] (/home/falguni/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/layer.js:95:5)
    at /home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/Documents/Build-A-Pinterest-Clone/node_modules/express/lib/router/index.js:335:12)

This is my code which i was working on (index.ejs):

<% layout('layout') %>


<div class="container">
  <div id="pins">
   <% for(var i = 0; i < pins.length; i++) { %>

  <div class="" style="margin:20px">
  <a href="/pins/details/<%= pins[i]._id %>"><img src="<%= pins[i].path %>" style="width: 200px" /></a>

  <h2><a href="/pins/details/<%= pins[i]._id %>"></a> <%= pins[i].title %></h2>

  <p>
    Submitted by <%= pins[i].username %>

    <a class="btn btn-default" href="/pins/delete/<%= pins[i]._id %>">DELETE</a>

    </p>

    </div>

    <% } %>

    </div>
    </div>

Besides this, this is the main methods: (pins.js):

var path = require('path');


var Pin = require('../models/pin');

// ../ previous directory




module.exports = function(app) {

    app.route('/pins/create')
   .get(function(req, res, next) {

    res.render('pins/create');


   })

   .post(function(req, res, next) {

    var pin = new Pin();

    pin.title = req.body.title;

    pin.desc = req.body.desc;

    pin.username = req.body.username;

    pin.isSave = false;


    if(!req.files)
        return json('error');

    let sampleFile = req.files.sampleFile;

    let fileName = Math.random().toString(26).slice(2) + '.jpg';

    let path = './public/Files/' + fileName;

    pin.path = '/Files/' + fileName;

    sampleFile.mv(path, function(err) {

        if(err)

            return res.status(500).send(err);



    })


    pin.save(function(err) {

        if(err) throw err;

        res.redirect('/pins/index');


    })
    
   })



    app.get('/pins/index', function(req, res, next) {
        Pin.find({}, function(err, pins) {
            res.render('pins/index', {pins: pins});
            
        })
    })


   //    app.get('/pins/delete/:id', function(req, res, next) {
   //    Pin.find({_id: req.params.id}.remove()
   //      .exec(function(err, foundPin){
   //        res.redirect('/pins/index');

   //      })




   // })


}

I was working on my index file page but suddenly this error is showing. Please can anyone help me.

bombom
  • 107
  • 2
  • 12
  • This ia my index.ejs code: – bombom Aug 04 '20 at 17:09
  • 1
    Presumably if `Pin.find` fails to find anything then the callback will be called with an `err` and `pins` will be undefined. Is that the case you are hitting? – Wyck Aug 04 '20 at 17:41
  • yes its causing error <% for(var i = 0; i < pins.length; i++) { %> this line – bombom Aug 04 '20 at 18:49
  • Yes, I know that's the line that's causing the error. What I asked is if `Pin.find` called its callback with a non-undefined `err` and with an undefined `pins`. Toss in a `console.log(err, pins)` before the `res.render` if you must. – Wyck Aug 04 '20 at 19:15

0 Answers0