简体   繁体   English

无法读取 null 的属性“id” - Javscript、EJS、MongoDB、Express

[英]Cannot read property 'id' of null - Javscript, EJS, MongoDB, Express

My error message is detailed below, which points to the code which I have also listed below.我的错误消息在下面详细说明,它指向我也在下面列出的代码。

For an overview, I am trying to create an edit function that allows you to edit a created post.作为概述,我正在尝试创建一个编辑功能,允许您编辑创建的帖子。 I have listed what I think to be the relevant code files below the error message that is directly below.我已经在直接位于下方的错误消息下方列出了我认为的相关代码文件。

I am fairly new to JS and using EJS, a MongoDB and express routes.我对 JS 和使用 EJS、MongoDB 和快速路由相当陌生。 Any help is greatly apprecaited!非常感谢任何帮助!

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

16|       Edit Post

17| 17| ?_method=PUT" method="POST"> ?_method=PUT" method="POST">

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

19|       </form>

20|     </div>

Cannot read property 'id' of null at eval (eval at compile (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:661:12), :10:13) at edit (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:691:17) at tryHandleCache (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:272:36) at View.exports.renderFile [as engine] (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:489:10) at 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) at Function.render (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\express\\lib\\application.js:592:3) at ServerResponse.render (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_无法在 eval 中读取 null 的属性“id”(编译时的 eval (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:661:12), :10:13)在编辑 (C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\node_modules\\ejs\\lib\\ejs.js:691:17) at 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) 在 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) at C:\\Users\\Dobson Dunavant\\Desktop\\Coding Course\\PersonalBlog_JRS\\routes\\post.js:13:7 at processTicksAndRejections (internal/process/task_queues.js:97:5) at runNextTicks (internal/process/task_queues.js:66:3) at processImmediate (internal/timers.js:429:9) 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)

//this is my Posts.js routes file //这是我的 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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM