[英]Sending data to mongodb realm gives 400 error
I am trying to post user data to my api from my application with the following code我正在尝试使用以下代码从我的应用程序将用户数据发布到我的 api
const submitPet = (e) => {
e.preventDefault();
let data = {
pet: petName,
breed: petBreed,
image: petImage,
desc: petDesc,
user: props.user
}
fetch('https://us-west-2.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/my_pets-dbdsd/service/pets/incoming_webhook/addnewpet', {
method: 'POST',
body: data,
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
}).then(function (response) {
if (response.ok) {
return response.json();
}
return Promise.reject(response);
}).then(function (data) {
console.log(data);
}).catch(function (error) {
console.warn('Something went wrong.', error);
});
}
My application is running in mongodb realm, and my webhook looks like so:我的应用程序在 mongodb realm 中运行,我的 webhook 如下所示:
exports = async function(payload, response) {
if (payload.body) {
const body = EJSON.parse(payload.body.text());
const reviews = context.services.get("mongodb-atlas").db("pets").collection("my_pets");
const reviewDoc = {
name: body.name,
user_id: body.user_id,
date: new Date(),
text: body.text,
};
return await reviews.insertOne(reviewDoc);
}
return {};
};
I've tested the webhook and it works from the mongodb console, I just can't get it to work from inside the application.我已经测试了 webhook,它在 mongodb 控制台上工作,我只是无法从应用程序内部工作。 No matter what method I use I get a POST 400 error.
无论我使用什么方法,我都会收到 POST 400 错误。 My full code is here https://github.com/Imstupidpleasehelp/MERN-lesson I appreciate your help in advance, I have been stuck on this for a while
我的完整代码在这里https://github.com/Imstupidpleasehelp/MERN-lesson我提前感谢您的帮助,我已经坚持了一段时间
This isn't a solution as more of a workaround, but when using axios I do not receive the error and the api works just fine.这不是解决方案,而是一种解决方法,但是在使用 axios 时,我没有收到错误,并且 api 工作得很好。 My new code for anyone interested
我的新代码给任何有兴趣的人
const submitPet = (e) => {
e.preventDefault();
let data = {
pet: petName,
breed: petBreed,
image: petImage,
desc: petDesc,
user: toString(props.user)
}
//'https://us-west-2.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/my_pets-dbdsd/service/pets/incoming_webhook/addnewpet'
axios
.post(
"https://us-west-2.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/my_pets-dbdsd/service/pets/incoming_webhook/addnewpet",
data
)
.then((response) => {
console.log(response);
});
}
I recreated this and got a 400 error as well, with the message:我重新创建了这个并得到了一个 400 错误,消息如下:
error: {"message":"invalid character 'o' looking for beginning of value","name":"TypeError"}
error_code: FunctionExecutionError
It made me think something was wrong with the format of the request sent, so I looked at the docs and they say that the body of a json request must be converted to json first with JSON.stringify
.这让我觉得发送的请求格式有问题,所以我查看了文档,他们说 json 请求的主体必须首先使用
JSON.stringify
。 Their example:他们的例子:
const data = { username: 'example' };
fetch('https://example.com/profile', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
After that it worked fine.之后它工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.