Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

node.js - I have created a node express server which worked fine in my local host environment but later when i deployed problem started

problem: POST and DELETE methods are not working after i host into heroku,i have used cors also and my database is mongodb. This POST and DELETE are sent to heroku as OPTIONS and returned status:204. Ihave found this in heroku logs.

const Delete=(e)=>{
        fetch(url/comment,{
            method: "DELETE",
            headers: {"Content-Type":"application/json",
            Authorization:`Basic ${credentials.username}:${credentials.password}`
        },
        body:JSON.stringify({post_id:props.match.params.id,comm_id:e})
        })
        window.location.reload("/post/"+e,true)
    }
question from:https://stackoverflow.com/questions/65869663/i-have-created-a-node-express-server-which-worked-fine-in-my-local-host-environm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

FindOneAndUpdate() and .exec() are depracated in the latest versions of mongoose so you will need to check which versions you're running.

Current version replaces FindOneAndUpdate() with updateOne() And exec() is not needed to execute anymore.

Also instead of having 2 responses

res.status(403); res.json({ message: "invalid credentials" });  

You can return both in just one response

res.status(403).json({ message: "invalid credentials" });

This should work just fine assuming that post_id and comm_id are valid

I put in a try{}catch so maybe it will return a useful error message if there is still an issue

Depractated version

   app.delete("/comment", async function (req, res) {
      const { authorization } = req.headers;
      const [token] = authorization.split(" ");
      const [username, password] = token.split(":");
      const {post_id, comm_id} = req.body.postItems;
      const user = await User.findOne({ username }).exec();
      if (!user || user.password !== password) {
        res.status(403).json({ message: "invalid credentials" });
        return ;
      }
      try{
        const post = await Post.FindOneAndUpdate(
          { _id: post_id },
          { $pull: { 'comments': { _id: comm_id } } }
        );
        res.status(200).json(post, + "Comment Deleted!")
      }catch(err){
        res.status(400).json({messsage: err})
      }
      
    });

Current version

app.delete("/comment", async function (req, res) {
  const { authorization } = req.headers;
  const [token] = authorization.split(" ");
  const [username, password] = token.split(":");
  const {post_id, comm_id} = req.body.postItems;
  const user = await User.findOne({ username });
  if (!user || user.password !== password) {
    res.status(403).json({ message: "invalid credentials" });
    return ;
  }
  try{
    const post = await Post.updateOne(
      { _id: post_id },
      { $pull: { comments: { _id: comm_id } } }
    );
    res.status(200).json(post, + "Comment Deleted!")
  }catch(err){
    res.status(400).json({messsage: err})
  }
  
});

I'm unsure on the exact anwser to your issue but hopefully this is of some use to you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...