0

I have data in MongoDB that I am trying to pass through to an ejs template through express and Mongoose - However when the data is rendered it behaves like it's undefined(I can't console.log the object I can't access it through any of my javascript files(I would like to access it in a separate javascript file that contains code that is centered around manipulating page elements not main app functionality) ) I am very new to programming I'm looking for a little guidance on how I would be able to take this mongoDB document and put it into a variable that I can manipulate with a javascript file. In order to reveal certain properties in the document as I see fit.

// app.js file that has express route to home page with mongoose model

var express = require("express"),
        app = express(),
 bodyParser = require("body-parser"),
   mongoose = require("mongoose");

mongoose.connect("mongodb://IP:PORT/users", { useNewUrlParser: true });
app.use(express.static("public"));
app.use(express.static("routes"));
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");




var technologySchema = new mongoose.Schema({
    Image: String,
        Name: String,
    desc: String
});

var Technology = mongoose.model("Technology", technologySchema);





app.get("/", function(req, res){


console.log(techList);
    Technology.find({}, function(err, alltechrecords){
        if(err){
            console.log(err);
        }else{
            res.render("home", {technologies: alltechrecords});
        }

});
  • 1. techList is undefined because you are not assigning any value to it if you want you can console.log(alltechrecords) before res.reder 2.you will find data in technologies.allrechrecords in ejs template – Ankit Halder May 04 '19 at 16:03
  • My apologies ankit, that was an old console.log that I forgot to take out - yes alltechords does console.log to my terminal but when I pass the object through to home.ejs and give it the value technologies then render it... I can't console.log from Chrome developer or separate javascript files linked in the home.ejs template I just get a message back saying technologies is undefined - however if I call on technologies in home.ejs and reference an index i.e. - technologies[2].Name it works - but I would like to access the entire object/document so I can manipulate what's showing with javascript – Caleb Fisher May 04 '19 at 18:08
  • to clarify - I'm wanting to use the document that I retrieve from MongoDB(the Technology.find{} call) and assign it to a variable that I can access in a separate javascript file that is linked to home.ejs - So how do I take the information that is passed into alltechrecords and make an array from it and assign it a variable that client side javascript could call on and manipulate different properties within it as I see fit.... Hope that makes sense - sorry if the explanation is confusing... – Caleb Fisher May 04 '19 at 18:19
  • can you share your ejs code ? – Ankit Halder May 04 '19 at 18:41
  • I guess that you want to access the entire mongoDb object that you retrieve from database in frontend (browser). if it's so, then you need to understand you can't access the object there because ejs conver the tag into html for ex. if you iterate data then it will be converted to html in backend and then rendered – Ankit Halder May 04 '19 at 18:50
  • you can refer to this if you want to share data between two ejs template [share data](https://stackoverflow.com/questions/29196562/how-to-include-a-template-with-parameters-in-ejs) – Ankit Halder May 04 '19 at 18:53

0 Answers0