'How to create an instance with nested array of object in node js

My model is

const profileSchema = new mongoose.Schema({
  fName: String,
  lName: String,
  address: String,
  webLinks: [
    {
      name: String,
      link: String,
    },
  ],
  },
  profileImage: String,
  images: [String],
  
  block: {
    type: Boolean,
    default: false,
  },

});

for the controller I have

The data I am getting from req.body for web links is in the following form(through form data of postman)

[ '{name:abc,link:link11.com}', '{name:def,link:link22.com}' ]

but when I am trying to create an instance using

const profile = await Profile.create({ fName, lName, address, webLinks, images })

The error I am getting is

validation failed: webLinks: Cast to embedded failed for value \"'{name:abc,link:link11.com}'\" (type string) at path \"webLinks\""


Solution 1:[1]

Kindly check the type of data inside the array it is a string instead of an object. hope I answered your question

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rajesh_kumar