Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda