简体   繁体   English

如何在 Express 中设置更新路由

[英]How to set up an update route in Express

I am building a MEVN stack CRUD app (Vue, Node, Express, MongoDB).我正在构建一个 MEVN 堆栈 CRUD 应用程序(Vue、Node、Express、MongoDB)。 I am attempting to set up the following Express route for my app...我正在尝试为我的应用设置以下 Express 路线...

postRoutes.post('/update/:id', async(req, res)=> {
    const collection = await loadPostsCollection();
    let id = req.params.id;
    let result = await collection.findOne(req.params.id)
    if (!result) {
      res.status(404).send("data is not found");
    }
    else {
        result.title = req.body.title;
        result.body = req.body.body;
        result.updateOne();
    }
});

..in order to update specific data based on the id of that data. ..为了根据该数据的 id 更新特定数据。 After clicking the update button on the front end and triggering an updatePost() method...单击前端的更新按钮并触发updatePost()方法后...

    updatePost() {
      let uri = `http://localhost:4000/posts/update/${this.$route.params.id}`;
      this.axios.post(uri, {
        title: this.post.title,
        body: this.post.body
      }).then(() => {
        this.$router.push({name: 'posts'});
      });
    }

...the express route above does not execute. ...上面的快递路线不执行。 I also tried configuring the route like so...我也试过像这样配置路由......

postRoutes.post('/update/:id', async(req, res)=> {
    const collection = await loadPostsCollection();
    let id = req.params.id;
    let result = await collection.findOne({
      _id: new mongodb.ObjectId(id)
    });
    if (!result) {
      res.status(404).send("data is not found");
    }
    else {
        result.title = req.body.title;
        result.body = req.body.body;
        result.updateOne();
    }
});

But this also did not work.但这也不起作用。 Any idea how to set up an update route?知道如何设置更新路线吗?

Here are all of my routes:这是我所有的路线:

const express = require('express');
const postRoutes = express.Router();
const mongodb = require('mongodb')

postRoutes.get('/', async (req, res) => {
  const collection = await loadPostsCollection();
  res.send(await collection.find({}).toArray());
});

postRoutes.post('/add', async (req, res) => {
  const collection = await loadPostsCollection();
  let task = req.body;
  await collection.insertOne(task);
  res.status(201).send();
});

postRoutes.get("/edit/:id", async (req, res) => {
  const collection = await loadPostsCollection();
  let id = req.params.id;
  let result = await collection.findOne({
    _id: new mongodb.ObjectId(id)
  });
  if (!result) {
    return res.status(400).send("Not found");
  }
  res.status(200).send(result);
});

postRoutes.post('/update/:id', async(req, res)=> {
    const collection = await loadPostsCollection();
    let id = req.params.id;
    let result = await collection.findOne({
      _id: new mongodb.ObjectId(id)
    });
    // let result = await collection.findOne(req.params.id)
    if (!result) {
      res.status(404).send("data is not found");
    }
    else {
        result.title = req.body.title;
        result.body = req.body.body;
        result.updateOne();
    }
});

postRoutes.delete('/delete/:id', async (req, res, next) => {
  const collection = await loadPostsCollection();
  collection.deleteOne({ _id: mongodb.ObjectId(req.params.id) });
  res.status(200).send({});
});

async function loadPostsCollection() {
  const client = await mongodb.MongoClient.connect(
    'mongodb+srv://jsfanatik:1qaz2wsx@cluster0-lv686.mongodb.net/test?retryWrites=true&w=majority',
    {
      useNewUrlParser: true
    }
  );
  return client.db("test").collection("todos")
}

module.exports = postRoutes;

You request URL is /posts/update/:id but I don't see that path in any express router.您请求的 URL 是/posts/update/:id但我在任何快速路由器中都没有看到该路径。 You are missin /posts in your router您在路由器中缺少 /posts

postRoutes.post('/update/:id', async(req, res)=> {
    const collection = await loadPostsCollection();
    let id = req.params.id;
    let result = await collection.findOne(req.params.id)
    if (!result) {
      res.status(404).send("data is not found");
    }
    else {
        result.title = req.body.title;
        result.body = req.body.body;
        result.updateOne();
    }
});

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

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