EJS中未定义如何修复对象

时间:2019-09-29 11:40:17

标签: javascript node.js express

我正在创建一个对象,并且正在创建一个包含到EJS的mongoDB数据的对象,但是奇怪的是该对象未定义,我该如何处理?

  

pinjam.js

const express = require('express')
const router = express.Router()
const Pinjam = require('../models/pinjam.model')

router.get('/', async (req, res) => {
  const pinjams = await Pinjam.find()
  res.render('index', {
    pinjams
  })
})

router.post('/add', async (req, res, next) => {
  try {
    const pinjam = new Pinjam(req.body)
    await pinjam.save()
    res.redirect('/')
  } catch (err) {
    console.log(err)
    next()
  }
})

router.get('/turn/:id', async (req, res, next) => {
  let {
    id
  } = req.params
  const pinjam = await Pinjam.findById(id)
  pinjam.status = !pinjam.status
  await pinjam.save()
  res.redirect('/')
})


router.get('/edit/:id', async (req, res, next) => {
  const pinjam = await Pinjam.findById(req.params.id)
  console.log(pinjam)
  res.render('edit', {
    pinjam
  })
})

router.post('/edit/:id', async (req, res, next) => {
  const {
    id
  } = req.params
  await Pinjam.update({
    _id: id
  }, req.body)
  res.redirect('/')
})

router.get('/delete/:id', async (req, res, next) => {
  let {
    id
  } = req.params
  await Pinjam.remove({
    _id: id
  })
  res.redirect('/')
})


module.exports = router
  

index.ejs

<% for(var i=0; i <  pinjams.length; i++) { %>
                <div class="baris mt-2">
                    <div class="row">
                        <div class="col-3">
                            <img src="img/MHL_0828.jpg" class="jaminan" alt="">
                        </div>
                        <div class="col-9">
                            <div class="row">
                                <div class="col-8">
                                    <div class="row">
                                        <h3><%=  pinjams[i].buku %></h3>
                                    </div>
                                    <div class="row mt-n1">
                                        <h5 id="keterangan">
                                            <%=  pinjams[i].nama %> - <%=  pinjams[i].kelas %>
                                        </h5>
                                    </div>
                                    <div class="row mt-n2">
                                        <h5 id="waktu">Dipinjams pada <%=  pinjams[i].waktu %> Sebanyak
                                            <%=  pinjams[i].jumlah %> buah</h5>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <a href="/turn/<%=  pinjams[i]._id %>"
                                        class="<%=  pinjams[i].status ? 'kembali-sudah' : 'kembali-belum' %>"><%=  pinjams[i].status ? 'Sudah dikembalikan' : 'Belum dikembalikan' %></a>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <% } %
  

这是错误

    ReferenceError: C:\Users\angga\OneDrive\Documents\project\perpus\views\index.ejs:28
    26|         <div class="tabel shadow">

    27|             <div class="mt-2" id="search_results ">

 >> 28|                 <% for(var i=0; i <  pinjams.length; i++) { %>

    29|                 <div class="baris mt-2">

    30|                     <div class="row">

    31|                         <div class="col-3">


pinjams is not defined
    at eval (eval at compile (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\ejs\lib\ejs.js:633:12), <anonymous>:22:26)
    at returnedFn (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\ejs\lib\ejs.js:668:17)
    at tryHandleCache (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\ejs\lib\ejs.js:254:36)
    at View.exports.renderFile [as engine] (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\ejs\lib\ejs.js:485:10)
    at View.render (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\response.js:1012:7)
    at router.get (C:\Users\angga\OneDrive\Documents\project\perpus\routes\index.js:9:57)
    at Layer.handle [as handle_request] (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\route.js:137:13)
    at forwardAuthenticated (C:\Users\angga\OneDrive\Documents\project\perpus\config\auth.js:11:20)
    at Layer.handle [as handle_request] (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\angga\OneDrive\Documents\project\perpus\node_modules\express\lib\router\layer.js:95:5)

我制作了2个具有相同内容的文件夹,第一个用于测试,并且在该项目中成功,并且发生了错误。我需要帮助!

0 个答案:

没有答案