首页 > 解决方案 > 无法读取 null 的属性 'id' - Javscript、EJS、MongoDB、Express

问题描述

我的错误消息在下面详细说明,它指向我也在下面列出的代码。

对于概述,我正在尝试创建一个允许您编辑创建的帖子的编辑功能。我在正下方的错误消息下方列出了我认为的相关代码文件。

我对 JS 相当陌生,并且使用 EJS、MongoDB 和快速路由。非常感谢任何帮助!

类型错误:C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\views\posts\edit.ejs:17 15|

16|       Edit Post

17| ?_method=PUT" 方法="POST">

18|         <%- include('_newpostform') %>

19|       </form>

20|     </div>

在 eval 时无法读取 null 的属性“id”(编译时的 eval (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\node_modules\ejs\lib\ejs.js:661:12),:10:13)在 tryHandleCache (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\ node_modules\ejs\lib\ejs.js:272:36) 在 View.exports.renderFile [作为引擎] (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\node_modules\ejs\lib\ejs.js: 489:10) 在 View.render (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\node_modules\express\lib\view.js:135:8) at tryRender (C:\Users\Dobson Dunavant\Desktop \Coding Course\PersonalBlog_JRS\node_modules\express\lib\application.js:640:10) 在 Function.render (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\node_modules\express\lib\application.js:592:3) 在 ServerResponse.render (C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\node_modules\express \lib\response.js:1012:7) 在 C:\Users\Dobson Dunavant\Desktop\Coding Course\PersonalBlog_JRS\routes\post.js:13:7 在 processTicksAndRejections (internal/process/task_queues.js:97:5 ) 在 runNextTicks (internal/process/task_queues.js:66:3) 在 processImmediate (internal/timers.js:429:9)5) 在 runNextTicks (internal/process/task_queues.js:66:3) 在 processImmediate (internal/timers.js:429:9)5) 在 runNextTicks (internal/process/task_queues.js:66:3) 在 processImmediate (internal/timers.js:429:9)

//这是我的 Posts.js 路由文件

       const express = require("express");
    const Post = require("../models/post");
    const router = express.Router();
    
    // app.set("view engine", "ejs");
    
    router.get("/new", (req, res) => {
      res.render("posts/new", { post: new Post() });
    });
    
    router.get("/edit/:id", async (req, res) => {
      const post = await Post.findById(req.params.id);
      res.render("posts/edit", { post: post });
    });
    
    router.get("/:slug", async (req, res) => {
      const post = await Post.findOne({ slug: req.params.slug });
      if (post == null) res.redirect("/");
      res.render("posts/show", { post: post });
    });
    
    router.post("/", async (req, res) => {
      let post = new Post({
        title: req.body.title,
        description: req.body.description,
        link: req.body.link,
      });
      try {
        post = await post.save();
        res.redirect(`/posts/${post.slug}`);
      } catch (error) {
        console.log("FAILURE");
        res.render("posts/show", { post: post });
      }
    });
    
    router.put("/:id", (req, res) => {});
    
    router.delete("/:id", async (req, res) => {
      await Post.findByIdAndDelete(req.params.id);
      res.redirect("/");
    });
    
    module.exports = router;


//**Here is my edit.ejs view file**
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
      crossorigin="anonymous"
    />
    <title>Gitfolio</title>
  </head>
  <body>
    <div class="new-post-container">
      Edit Post
      <form action="/posts/<% post.id %>?_method=PUT" method="POST">
        <%- include('_newpostform') %>
      </form>
    </div>
  </body>

  <style></style>
</html>

标签: javascriptmongodbexpressejs

解决方案


为什么你使用 post-method 来更新现有项目而不是 put?


推荐阅读