boka18
boka18

Reputation: 317

NodeJs can't return data from MongoDB to frontpage

This is my home.js router :

    var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();


var Movie = require('../models/movie');
var urlencodedParser = bodyParser.urlencoded({extended: false});


// Get Homepage
router.get('/', function(req, res){
    //get data from mongodb and pass it to view
    Movie.find({}, function(err, data){
        if (err) throw err;
        res.render('home', {movies: data});
        });
    });

function ensureAuthenticated(req, res, next){
    if(req.isAuthenticated()){
        return next();
    } else {
        //req.flash('error_msg','You are not logged in');
        res.redirect('/users/login');
    }

};
module.exports = router;

this is my models/movie

    var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');

// Movie Schema
var MovieSchema = mongoose.Schema({
    movieName: {
        type: String,
        index:true
    },
    img: {
        type: String
    },
    description: {
        type: String
    },
});

var Movie = module.exports = mongoose.model('Movie', MovieSchema);

this is my homepage

    <h2 class="page-header">Резервирај Билет</h2>
<div class="movies-block">
  <div class="movie">
    <h3>Movie #1<h3>
    <img src="https://upload.wikimedia.org/wikipedia/en/c/c2/WALL-Eposter.jpg" width="150px" height="200px"></img></br>
    <button type="button" name="rezerviraj">Резервирај</button>
  </div>

  <div class="movie">
    <ul>
      <% for(var i=0; i<movies.length; i++){ %>
      <li><%= movies[i].item %></li>
      <% } %>
    </ul>
</div>

So, I can send data to db, but I can't retrieve it. I want to put out the data that is in my MongoDB to the front page. The goal is: I log in in the admin dashboard, add movie details (Movie name, pic and description). When I submit it, it goes to the MongoDB. From there, the front page requests the data and puts it where i want it. For now, i just get this code on the front page:

<% for(var i=0; i
<%= movies[i].item %>
<% } %>

Where am I making my mistake?

Upvotes: 0

Views: 60

Answers (1)

user7126329
user7126329

Reputation:

This is a comment, but I don't have enough rep to comment.

For now, i just get this code on the front page <% for(var i=0; i <%= movies[i].item %> <% } %>

You are using EJS template engine, are you sure you already set the view engine express app to ejs? It looks like not, since the template hasn't render the for expression.

Upvotes: 1

Related Questions