Thanks for the help in advance.
I am trying to retrieve data from my database- myFirstDatabase and collection named as 'shipment' in mongondb. This is a nested schema but I am only interested in the parent data for now. I have this code which retrieves data to the console log. But how can I display or access the data in my orders.ejs file?
Shipment.find({}, function (err, data) {
if (err) throw err
console.log(data)
})
MongoDB connected...
[
{
_id: new ObjectId("61353311261da54811ee0ca5"),
name: 'Micky Mouse',
phone: '5557770000',
email: 'g@gmail.com',
address: {
address: '10 Merrybrook Drive',
city: 'Herndon',
state: 'Virginia',
zip: '21171',
country: 'United States',
_id: new ObjectId("61353311261da54811ee0ca6")
},
items: {
car: 'Honda Pilot 2018',
boxItem: '3 uHaul boxes',
furniture: 'None',
electronics: '1 50" Samsung TV',
bags: '2 black suites cases',
_id: new ObjectId("61353311261da54811ee0ca7")
},
date: 2021-09-05T21:13:53.484Z,
__v: 0
}
]
This is the ejs file, a table I am trying to populate the data i get from my mongodb
<div class="mt-5 m-auto>
<h3 class="mt-5">This is the order table</h3>
<%- include ("./partials/messages"); %>
<div class="col-sm">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Customer</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Phone</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td>1</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
server.js
const express = require('express')
const mongoose = require('mongoose')
const Shipment= require('./models/shipment')
const app = express()
mongoose.connect('mongodb://localhost/myFirstDatabase ', {
useNewUrlParser: true, useUnifiedTopology: true
})
app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended:false }))
app.get('/', async (req,res) => {
const data= await Shipment.find()
res.render('index', { data: data})
})
app.listen(process.env.PORT || 5000);
index.ejs
below is part of your ejs file
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Customer</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Phone</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% data.forEach((e, index)=> { %>
<tr>
<td><%= index %></td>
<td><%= e.Customer %></td>
<td><%= e.Address %></td>
<td><%= e.City %></td>
<td><%= e.State %></td>
<td><%= e.Zip %></td>
<td><%= e.Phone %></td>
<td><%= e.Status %></td>
</tr>
<% }) %>
</tbody>
</table>