-2

there is my frontend code in react js .....

const onClickHandler = () => {
   const data = new FormData()
   for(var x = 0; x<selectedFile.length; x++) {
       data.append('file', selectedFile[x])
   }
  let post = state.postData
  data.append("obj", JSON.stringify(post))
  Axios.post("http://localhost:4000/postImages", data)

.then(res => console.log("response>>>",res))

}

there is my nodeJs server code and it is work correctely and give me correct response.....

router.post("/", upload, function(req, res, next) {
  const obj = JSON.parse(req.body.obj)

    let postData = {
         file: req.files,
         data: obj,
      };
      res.send(postData)
});

but when i am use schema with above node js code then accord error in respons that is "xhr.js:184 POST http://localhost:4000/postImages 500 (Internal Server Error)". there is show in below "error Code"

router.post("/", upload, function(req, res, next) {
  const obj = JSON.parse(req.body.obj)

    let newPostData = new newPostSchema({
    id: obj.id,
    name: obj.name,
    price: obj.price,
    salePrice: obj.salePrice,
    discount: obj.discount,
    pictures: obj.pictures,
    shortDetails: obj.shortDetails,
    description: obj.description,
    stock: obj.stock,
    new: obj.new,
    sale: obj.sale,
    category: obj.category,
    colors: obj.colors,
    size: obj.size,
    tags: obj.tags,
    rating: obj.rating,
    variants: obj.variants,
    });
    let postData = {
         file: req.files,
         data: newPostData,
      };
      res.send(postData)
});

here is my code schema .....

const mongoose = require("mongoose");

const newPost_Schema = new mongoose.Schema(
  {
    id: Number,
    name: String,
    price: Number,
    salePrice: Number,
    discount: Number,
    pictures: [String],
    shortDetails: String,
    description: String,
    stock: Number,
    new: Boolean,
    sale: Boolean,
    category: String,
    colors: [String],
    size: [String],
    tags: [String],
    rating: Number,
    variants: [Object],
  },
  {
    timestamps: true,
  }
);
const newPostSchema = mongoose.model("newPost_data", newPost_Schema);
exports.newPostSchema = newPostSchema;
Rizwan Ali
  • 955
  • 2
  • 5
  • 7
  • wrap your `outer.post("/", upload, function(req, res, next) {` with try and catch and post the error you are getting . – ddor254 Sep 30 '21 at 09:21
  • bundel of THANKS ddor254, i foun my error through { try and catch method } . my require() schema was commented. Thanks a lot for help......... – Rizwan Ali Oct 01 '21 at 04:07

1 Answers1

0

there is no error. i am using just try and catch method and i found small error that was commented my require schema file...

// const { newPostSchema } = require("../all_schemas/newPostSchema");

that was the reson of my error......

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
Rizwan Ali
  • 955
  • 2
  • 5
  • 7