节点js no上传formdata在react本地的服务器中,在react js中工作正常

发布于 2025-01-27 19:41:15 字数 1646 浏览 1 评论 0原文

嗨,我非常需要使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2025-02-03 19:41:15

我发现我的问题问题的答案是在服务方面:

我没有正确创建一个静态文件夹,后来我添加了下行

app.use(cors());
app.use('/image',express.static("uploads"));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
 bodyParser.urlencoded({
   // to support URL-encoded bodies
   extended: false,
 })
); 

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

app.use(cors());
app.use('/image',express.static("uploads"));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
 bodyParser.urlencoded({
   // to support URL-encoded bodies
   extended: false,
 })
); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文