I am trying to display data from sample collection thats in mongoDB Atlas. i have connected to the server, retrieved the data. But the problem is i cannot choose specific data. if i do it says undefined.
Here is the pic and code for better understanding:
MY MOVIE MODEL movie.js
const mongoose = require("mongoose");
const { Schema } = mongoose;
require("dotenv").config();
const mongoDB_API = process.env.MONGODB_API_KEY;
const mflixDB = mongoose.createConnection(mongoDB_API).useDb("sample_mflix");
const Movies = mflixDB.model("Movie", new Schema({}), "movies");
module.exports = Movies;
I used the code via mongoose access preexisting collection
my app.js
const express = require('express');
const bodyParser = require('body-parser');
const _ =require('lodash');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static('public'));
const Movies = require('./model/mflix/movies');
app.get("/movies",async (req, res) => {
const ourMovies = await Movies.find().sort({'date': -1}).limit(2);
console.log("WE recieved: \n")
console.log(ourMovies);
const titles =[];
ourMovies.forEach( x=>{
console.log(x._id, x.title)
})
res.render("movies", { recievedList: ourMovies });
});
As u can see x.title is undefined instead of respective title.
I cannot access any info other than _id. Is this because i didn't properly defined my schema for the model ? How do i fix this?