0

I'm store array of object but it is giving mer following error.

(node:87929) UnhandledPromiseRejectionWarning: ValidationError: Place validation failed: placesToVisit: Cast to embedded failed for value "[{"name":"xczc","location":"zczxc","description":"czxczx"}]" (type string) at path "placesToVisit" because of "ObjectParameterError"

here is my schema:

placesToVisit: [
    {
      name: {
        type: String,
        required: [true, "Please enter the place's name"],
      },
      description:{
        type: String,
        required: [true, "Please enter the place's description"],
      },
      location:{
        type: String,
        required: [true, "Please enter the place's location"],
      }
    },
  ],

here is my contoller:

exports.createPlace = async (req, res) => {
  
  try {
    const newPlace = await new Place({
    
     placesToVisit:req.body.placesToVisit,
    
    });
    newPlace.save()
    debugger
    res.status(201).json({
        success:true,
        message:"Place Created Successfully",
        newPlace
    })
  } catch (error) {
     return res.status(500).json({
          success:false,
          error:error.message
      })
  }
};

here is my formdata structure:

data.append("placesToVisit",JSON.stringify(values.placesToVisit))

here is ss of yaload enter image description here

1 Answer 1

2

You must convert the string to an array:

placesToVisit: JSON.parse(req.body.placesToVisit),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.