Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误状态:404使用Node.js(express)、Angular和MongoDB返回PUT请求_Node.js_Angular_Mongodb_Express_Mongoose - Fatal编程技术网

错误状态:404使用Node.js(express)、Angular和MongoDB返回PUT请求

错误状态:404使用Node.js(express)、Angular和MongoDB返回PUT请求,node.js,angular,mongodb,express,mongoose,Node.js,Angular,Mongodb,Express,Mongoose,我一直在想如何将我的数据(“Post”)更新到MongoDB(我使用的是Mongoose、MongoDB、Node、Express和Angular 9.1.1)。我在POST和DELETE方面没有问题,但我无法找出PUT的错误所在 提前感谢,非常感谢您的帮助-杰夫 角度:文件post.ts export class Post { id: string; title: string; message: string; } 角度:文件postsService.ts //W

我一直在想如何将我的数据(“Post”)更新到MongoDB(我使用的是Mongoose、MongoDB、Node、Express和Angular 9.1.1)。我在POST和DELETE方面没有问题,但我无法找出PUT的错误所在

提前感谢,非常感谢您的帮助-杰夫

角度:文件post.ts

export class Post {
    id: string;
    title: string;
    message: string;
 }
角度:文件postsService.ts

//Working OK
deletePost(postId: string) {
    this.http.delete(`http://localhost:3000/api/posts/${postId}`)
        .subscribe(() => {
            const updatedPosts = this.posts.filter(post => post.id !== postId);
            this.posts = updatedPosts;
            this.postsUpdate$.next([...this.posts]);
        });
    }

//Not working - I want to update one post
updatePost(postId: string, postTitle: string, postMessage: string) {
    const post: Post = { id: postId, title: postTitle, message: postMessage };
    this.http.put(`http://localhost:3000/api/posts/${postId}`, post)
        .subscribe(response => console.log(response));
    }
节点服务器:文件backend/app.js

// Working OK
app.delete("/api/posts/:id", (req, res, next) => {
    Post.deleteOne({ _id: req.params.id }).then((result) => {
        console.log(req.params.id);
        res.status(200).json({ msg: "Post deleted successfully!" });
    });
});

//Not working - I want to update one post
// Post is defined in file: backend/model/post.js
app.put("/api/post/:id", (req, res, next) => {
    const post1 = new Post({
        _id: req.body.id,
        title: req.body.title,
        message: req.body.message,
    });
Post.findOneAndUpdate({ _id: req.params.id } ,post1)
    .then((result) => {
    console.log(result);
    res.status(200).json({ msg: "Updated successfully!" });
    });
});

节点:backend/model/post.js

const mongoose = require("mongoose");

//note "String is a class used in node/mongoDB"
const postSchema = mongoose.Schema({
    title: { type: String, required: true },
    message: { type: String, required: true },
});
// this will automaticly be stored in collection "posts"
module.exports = mongoose.model('Post', postSchema);

浏览器中出现错误:

// HEADERS:
Request URL:http://localhost:3000/api/posts/5ef676c71105924a08b9e919
Request Method:PUT
Remote Address:127.0.0.1:3000
Status Code:
404
Version:HTTP/1.1
Referrer Policy:strict-origin-when-cross-origin

// REQUEST:
 
Request Payload:
{"id":"5ef676c71105924a08b9e919", "title":"first Post ", "message":"This is just some text"}

// RESPONCE:
Cannot PUT /api/posts/5ef676c71105924a08b9e919
但在URL上,在GET上一切都很好:

{"msg":"Post fetched successfully!","posts":{"_id":"5ef676c71105924a08b9e919","title":"first Post ","message":"This is just some text","__v":0}}

似乎你正在尝试更新一篇文章的记录,所以你可以简单地尝试一下

router.put('/api/post/:id', async (req, res, next) => {
    return Post.updateOne({
            _id: req.params.id
        }, {
            $set: {
                title: req.body.title,
                message: req.body.message
            }
        })
        .then(result => res.json(result))
        .catch(err => console.log(err))
});

不需要创建新的Post,因为您正试图根据
id
更新现有记录,并且您正在发送类似
api/Post
的路由路径,但是您收到服务器
api/posts
的响应,因此也要检查您的路由路径。

您的api put请求指向错误的url 棱角的

后端

app.put("/api/post/:id", (req, res, next) => {


一个是帖子,另一个是帖子。它们必须是一样的。

非常感谢-就是这样-请注意,不要太累;)不客气。像这样的东西几乎总是一个你看不到的打字错误,因为你知道应该有什么,所以你看不到有什么。干杯,这是不匹配的路径。感谢您展示完整的错误处理代码。我会试试的
app.put("/api/post/:id", (req, res, next) => {