javascript - 无法读取 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>
解决方案
为什么你使用 post-method 来更新现有项目而不是 put?
推荐阅读
- swift - 使用 Core Graphics/DisplayLink 的持久非清除视图
- excel - 在二维数组中查找唯一值
- react-native - 来自 useEffect 的异步自定义钩子
- python - 使用 ODE 通过 scipy.integrate.solve_ivp 绘制粒子运动
- swift - 在 SwiftUI 中初始化之前使用的变量“varname”
- angular - glob-parent 中的漏洞:angular-devkit、angular-compiler、eslint - 它会影响产品构建吗?
- azure - 未经邀请使用 SAML 的 Azure AD 外部身份
- c++ - 你能找到多重集的哪个实例包含给定的迭代器吗?
- javascript - 显示更多/更少具有响应行为的文本
- android - 如何在编辑文本中将图标居中?