运行更新时出现猫鼬nodejs错误

时间:2019-04-24 11:39:43

标签: node.js express mongoose

我正在尝试使用crud,但是在nodejs猫鼬中遇到了参考错误。 错误: enter image description here 请帮忙 我正在nodejs和猫鼬中尝试CRUD。由于未定义管理员错误 adminController

router.get('/edit/:id', async (req,res) => {
    const { id } = req.params;
    const admin = await Admin.findById(id);
    res.render('table', {
        admin
    });
});

router.get('/edit/:id', async (req,res) => {
    const { id } = req.params;
    await Admin.update({_id: id}, req.body);
    res.redirect('/seo');
});

table.ejs

<a href="/edit/<%= admins[i]._id%>" class="btn btn-primary" data-toggle="modal" data-target="#editModal" class="modal-btn" id="btn_modal">Edit</a>
<div class="modal" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="editModalLabel">Edit</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="/edit <%= admin._id %>" method="POST" id="myForm">
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Title" name="title" value="<%= admin.title%>">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" placeholder="URL" name="url" value="<%= admin.url%>">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Description" name="description" value="<%= admin.description%>">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Keywords" name="keywords" value="<%= admin.keywords%>">
          </div>
        </form>

1 个答案:

答案 0 :(得分:0)

您将需要通过admins,例如:

res.render('table', {
    admins: [admin]
});