简体   繁体   English

POST http://localhost:3000/updateSurvey 400(错误请求)

[英]POST http://localhost:3000/updateSurvey 400 (Bad Request)

I am trying to update a collection in mongoDB after the user finishes some tasks.在用户完成一些任务后,我正在尝试更新 mongoDB 中的集合。 However, whenever I attempt to save the information and update mongo, I'm getting the error POST http://localhost:3000/updateSurvey/634124db6f 400 (Bad Request) .但是,每当我尝试保存信息并更新 mongo 时,都会收到错误POST http://localhost:3000/updateSurvey/634124db6f 400 (Bad Request) Any ideas why my code isn't functioning correctly?任何想法为什么我的代码无法正常运行?

Backend js script后端js脚本

app.post('/updateSurvey', async (req, res) => {
try {
    await client.connect();
    var db = client.db('Admin_Db');
    var collection = db.collection('Survey');

    await collection.findOneAndUpdate({"_id": ObjectId(req.body.id)}, {completion: req.body.completion});
    res.send("updated");
} catch (error) {
    console.log(error);
    res.status(400).send(error);
}
});

Main.js (this is how I am fetching the data from mongo) Main.js(这就是我从 mongo 获取数据的方式)

fetch("http://localhost:3000/updateSurvey", {
method:'POST', 
headers:{'Content-Type': 'application/json'}, 
body: JSON.stringify({id: surveyID, completion: true})})
.then(response=>{response.text()})
.catch(function(err){console.log("Fetch Problem: " + err)});

You don't have a route http://localhost:3000/updateSurvey/634124db6f exposed on your server.您的服务器上没有公开路由http://localhost:3000/updateSurvey/634124db6f Therefore the 404 error.因此出现 404 错误。 Since you are using a post call, just pass the surveyID in your body when making the post call using fetchAPI instead of sending it as a query param.由于您使用的是 post 调用,因此在使用 fetchAPI 进行 post 调用时只需在您的 body 中传递surveyID ,而不是将其作为查询参数发送。

And make sure http://localhost:3000/updateSurvey is the route to which your are sending your data, without the surveyId in the URL.并确保http://localhost:3000/updateSurvey是您发送数据的路径,URL 中没有surveyId。

Edit编辑

Edits made as per request received in comments.根据评论中收到的请求进行的编辑。

collection.findOneAndUpdate({"_id": id)}, {completion: req.body.completion});

should be:应该:

collection.findOneAndUpdate({"_id": new ObjectId(id))}, {completion: req.body.completion});

_id is of type ObjectId in MongoDB. _id 是 MongoDB 中的 ObjectId 类型。 You are passing id as string.您将 id 作为字符串传递。 This should most likely be the error from what I can gather by the code shared in your question.这很可能是我可以通过您问题中共享的代码收集到的错误。 You can cast a string to ObjectId by using the ObjectId class provided by the Mongo Driver for node.您可以使用 Mongo Driver for node 提供的 ObjectId class 将字符串转换为 ObjectId。 Even mongoose provides this class.甚至 mongoose 也提供了这个 class。

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

相关问题 POST http://localhost:3000/api/signup 400(错误请求) - POST http://localhost:3000/api/signup 400 (Bad Request) 尝试将数据从客户端发送到服务器时,POST http://localhost:3000/data 400(错误请求) - POST http://localhost:3000/data 400 (Bad Request) when trying to send data from client to server 在 ejs 和 nodejs 中发布 http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQUneY3 400(错误请求) - POST http://localhost:3000/socket.io/?EIO=3&transport=polling&t=NQUneY3 400 (Bad Request) in ejs and nodejs POST http://localhost:7500/api/users/posts 400(错误请求) - POST http://localhost:7500/api/users/posts 400 (Bad Request) HTTP POST -> 400:错误请求 - HTTP POST -> 400: Bad Request http://localhost:8000/token/ 400(错误请求) - http://localhost:8000/token/ 400 (Bad Request) POST http:// localhost:51348 / TestService.svc / SendCredentials 400(错误请求) - POST http://localhost:51348/TestService.svc/SendCredentials 400 (Bad Request) $ http发布方法的400错误请求 - 400 Bad Request for $http post method 我如何将 http post 请求从 localhost:5000 发送到 localhost:3000 - how can i send http post request from localhost:5000 to localhost:3000 Flask 和 Ajax 发布 HTTP 400 错误请求错误 - Flask and Ajax Post HTTP 400 Bad Request Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM