代码之家  ›  专栏  ›  技术社区  ›  kinhs

为什么在我的node.js应用程序中创建新注释不起作用?

  •  0
  • kinhs  · 技术社区  · 5 年前

    我正试图在node.js应用程序中创建新的注释,但它返回了错误。实际上,我正试图使用我的帖子路径在特定帖子下创建新的评论。以下是我的代码:

    注释模型

      const mongoose = require("mongoose"); //import mongoose
     const Schema = mongoose.Schema;
    
     const CommentSchema = new mongoose.Schema(
     {
       
        title:{
            type: String,
            required: true,
            unique: true
        },
        description:{
            type: String,
            required: true, 
        },
       username:{
            type: Schema.Types.ObjectId, 
            ref: 'User'
        },
       
      }, {timestamps: true}
       );
     //exporting this schema
     module.exports = mongoose.model("Comment", CommentSchema); //the module name is "Post"
    

    评论路线

    const router = require("express").Router()
    const Comment = require("../models/Comment")
    
    router.post("/posts/:id/comment", async (req, res) =>{
    const newComment = new Comment(req.body);//we create a new category for the database
        
    try{
        const savedComment = await newComment.save();
        res.status(200).json(savedComment)
    }catch(err){
        res.status(500).json(err)
    }
    })
    

    索引.js

    const express = require("express");//creating the express server
    const app = express(); //creating the application
    const mongoose = require("mongoose");
    
    app.use("/api/posts/:id", commentRoute)
    

    如果我的目标不是“/posts/:id/comment path和targeted comment root path”/“,则在poster中可以创建评论。但我正试图使用post:id在帖子内创建评论。这不起作用。它将此错误推到poster上:

    邮差错误

    <head>
    <meta charset="utf-8">
    <title>Error</title>
    </head>
    
    <body>
    <pre>Cannot POST /api/posts/6137668b697dd2448c3f454c/comment</pre>
    </body>
    

    我附上了我的邮递员错误和设置的屏幕截图 enter image description here

    0 回复  |  直到 5 年前
        1
  •  0
  •   GabrielMC    5 年前

    按照你目前设置路线的方式,它希望你的路线看起来是这样的:

    /api/posts/:id/posts/:id/comment <---这不是你想要的

    当执行commentRoute文件中的代码时,路径已经位于 /api/posts/:id ,所以你的评论路线只需要检查路径的最后一部分 /comment .

    重写你的评论路线,使其看起来更像这样:

    // ------------------------- /api/posts/:id ------------------
    
    const router = require("express").Router()
    const Comment = require("../models/Comment")
    
    router.post("/comment", async (req, res) => {
         const newComment = new Comment(req.body);
        
         try{
             const savedComment = await newComment.save();
             res.status(200).json(savedComment);
         } catch(err) {
             res.status(500).json(err);
         }
    });
    

    注释 :我个人喜欢把这个注释放在我所有路由文件的顶部,这样当它到达这个文件中的代码时,我就知道路径是什么样子了。

        2
  •  0
  •   Wyck    5 年前

    因为您将路由器安装在 /api/posts/:id ,那么您应该更改:

    router.post("/posts/:id/comment", async (req, res) =>{
    

    到

    router.post("/comment", async (req, res) =>{
    

    以及变化:

    const router = require("express").Router()
    

    到

    const router = require("express").Router({ mergeParams: true })
    

    然后你的 id 可在获得 req.params.id 在的处理程序中 /comment 。例如:

    router.post("/comment", async (req, res) => {
      console.log(`Commenting on ${req.params.id)}`);
      // ...
    
    推荐文章