Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
How to retrieve data from MongoDb Atlas and display in an ejs file using mongoose and Nodejs

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>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda