如何在Express中设置更新路线 [英] How to set up an update route in Express
问题描述
我正在构建一个MEVN堆栈CRUD应用程序(Vue,Node,Express,MongoDB).我正在尝试为我的应用设置以下Express路线...
I am building a MEVN stack CRUD app (Vue, Node, Express, MongoDB). I am attempting to set up the following Express route for my app...
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();
}
});
..以便根据该数据的ID更新特定数据.单击前端的更新按钮并触发updatePost()
方法后...
..in order to update specific data based on the id of that data. After clicking the update button on the front end and triggering an updatePost()
method...
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?
这是我所有的路线:
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;
推荐答案
您请求的URL是/posts/update/:id
,但我在任何快速路由器中都看不到该路径.
您是路由器中的Missin/帖子
You request URL is /posts/update/:id
but I don't see that path in any express router.
You are missin /posts in your router
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();
}
});
这篇关于如何在Express中设置更新路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!