【问题标题】:How to retrieve data from MongoDb Atlas and display in an ejs file using mongoose and Nodejs如何使用 mongoose 和 Nodejs 从 MongoDb Atlas 检索数据并显示在 ejs 文件中
【发布时间】:2021-11-03 02:56:39
【问题描述】:

提前感谢您的帮助。

我正在尝试从我的数据库 myFirstDatabase 和 mongondb 中名为“shipment”的集合中检索数据。这是一个嵌套模式,但我现在只对父数据感兴趣。我有这段代码可以将数据检索到控制台日志。但是如何显示或访问我的 orders.ejs 文件中的数据?

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
  }
]

这是 ejs 文件,我正在尝试填充从我的 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>

【问题讨论】:

  • 你在使用框架吗? Shipment 调用是否在快速请求/响应函数中?
  • 我正在使用 Express 路由处理程序。
  • 看玉婷的回答。就是这样。

标签: javascript node.js mongodb express mongoose


【解决方案1】:

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

以下是您的 ejs 文件的一部分

<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>

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2017-05-10
    • 1970-01-01
    • 2020-05-16
    • 2018-03-15
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多