节点js no上传formdata在react本地的服务器中,在react js中工作正常
嗨,我非常需要使用Expo React Native开发的MERN stack应用程序的帮助。我正在使用字符串类型表单数据将我的图像文件上传到数据库中,通过将image上传到文件夹并保存在数据库中,我的代码在React中工作正常JS应用程序,但它在使用React Native的情况下给Postman提供了404个不良请求错误,这是我的代码片段:
petRoute.route('/pets/addpets').post( upload.any(), (req, res) => {
try {
if (
req.files &&
req.body &&
req.body.name &&
req.body.description &&
req.body.price &&
req.body.title &&
req.body.selectedcity &&
req.body.contact &&
req.body.selectedcat
) {
let new_product = new petModel();
new_product.name = req.body.name;
new_product.description = req.body.description;
new_product.price = req.body.price;
new_product.imgforsell = req.files[0].filename;
new_product.title = req.body.title;
new_product.selectedcat = req.body.selectedcat;
new_product.contact = req.body.contact;
new_product.selectedcity = req.body.selectedcity;
//new_product.user_id = req.user.id;
new_product.save((err, data) => {
if (err) {
res.status(400).json({
errorMessage: err,
status: false,
});
} else {
res.status(200).json({
status: true,
title: "Product Added successfully.",
});
}
});
} else {
res.status(400).json({
errorMessage: "Add proper parameter first!",
status: false,
});
}
} catch (e) {
res.status(400).json({
errorMessage: "Something went wrong!",
status: false,
});
}
});
Hi there I seriously need help for my mern stack application developed using expo react native.I am using string type form data to upload my image file to data base by uploading image to folder and saving name in data base my code is working fine in react js application but it is giving 404 bad request error in postman while using it with react native here is my code snippet:
petRoute.route('/pets/addpets').post( upload.any(), (req, res) => {
try {
if (
req.files &&
req.body &&
req.body.name &&
req.body.description &&
req.body.price &&
req.body.title &&
req.body.selectedcity &&
req.body.contact &&
req.body.selectedcat
) {
let new_product = new petModel();
new_product.name = req.body.name;
new_product.description = req.body.description;
new_product.price = req.body.price;
new_product.imgforsell = req.files[0].filename;
new_product.title = req.body.title;
new_product.selectedcat = req.body.selectedcat;
new_product.contact = req.body.contact;
new_product.selectedcity = req.body.selectedcity;
//new_product.user_id = req.user.id;
new_product.save((err, data) => {
if (err) {
res.status(400).json({
errorMessage: err,
status: false,
});
} else {
res.status(200).json({
status: true,
title: "Product Added successfully.",
});
}
});
} else {
res.status(400).json({
errorMessage: "Add proper parameter first!",
status: false,
});
}
} catch (e) {
res.status(400).json({
errorMessage: "Something went wrong!",
status: false,
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现我的问题问题的答案是在服务方面:
我没有正确创建一个静态文件夹,后来我添加了下行
I have found answer to my question problem was on service side:
I had not properly created a static folder which I later added following line