(节点:6316)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

时间:2020-08-22 12:59:15

标签: javascript node.js mongodb express mongoose

我正在尝试使用PUT方法将表单数据编辑和更新到MongoDB数据库。但是,当我尝试编辑并单击表单中的“保存”按钮时,它抛出以下错误:

(node:6316) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at router.put (C:\Users\mypc\Desktop\Javascript\BlogApp\routes\articles.js:38:53)
    at Layer.handle [as handle_request] (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:281:22
    at param (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:354:14)
    at param (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:410:3)
    at next (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\mypc\Desktop\Javascript\BlogApp\node_modules\express\lib\router\index.js:335:12)

路由器代码( articles.js ):

const express = require("express");
const mongoose = require("mongoose");
const articleRouter = require("./routes/articles");
const Article = require("./models/article.model");

router.put("/:id", async (res, req) => {
  const article = await Article.findById(req.params.id);
});

下面是我的模型/架构代码( article.model.js

const mongoose = require("mongoose");
const marked = require("marked");
const slugify = require("slugify");
const createDomPurify = require("dompurify");
const { JSDOM } = require("jsdom");
const dompurify = createDomPurify(new JSDOM().window);

const articleSchema = new mongoose.Schema({
  title: {
    type: String,
    require: true,
  },
  description: {
    type: String,
    require: true,
  },
  markdown: {
    type: String,
    require: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
  slug: {
    type: String,
    unique: true,
  },
  sanitizedHtml: {
    type: String,
    required: true,
  },
});

articleSchema.pre("validate", function (next) {
  if (this.title) {
    this.slug = slugify(this.title, { lower: true, strict: true });
  }

  if (this.markdown) {
    this.sanitizedHtml = dompurify.sanitize(marked(this.markdown));
  }

  next();
});

module.exports = mongoose.model("Blog_Article", articleSchema);

请帮帮我。我需要优先提交此作业:)

1 个答案:

答案 0 :(得分:0)

似乎reqrep参数的顺序需要更改。

router.put("/:id", async (req, res) => {