admin管理员组

文章数量:1314033

i am getting Cast to string failed for value error

see here the code to initalised that data

const initDB = async ()=>{
    await Listing.deleteMany({}) ;
    await Listing.insertMany(initData.data) ;
    console.log("Data is initalised") ;
};

initDB() ;

here is the listing schema

const listingSchema = new Schema({
  title: {
    type: String,
    required: true,
  },
  discription: String,
  image: {
    type: String,
    default:
      ";auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHw2OXx8fGVufDB8fHx8fA%3D%3D",
    set: (v) =>
      v === ""
        ? ";auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHw2OXx8fGVufDB8fHx8fA%3D%3D"
        : v,
  },
  price: Number,
  location: String,
  country: String,
});

here the error message

ValidationError: Listing validation failed: image: Cast to string failed for value "{
  filename: 'listingimage',
  url: '.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fHRyYXZlbHxlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=800&q=60'
}"

here the sample data i am trying to intialise

{
    title: "Secluded Beach House in Costa Rica",
    description:
      "Escape to a secluded beach house on the Pacific coast of Costa Rica. Surf, relax, and unwind.",
    image: {
      filename: "listingimage",
      url: ".0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhY2glMjBob3VzZXxlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=800&q=60",
    },
    price: 1800,
    location: "Costa Rica",
    country: "Costa Rica",
  },

i am getting Cast to string failed for value error

see here the code to initalised that data

const initDB = async ()=>{
    await Listing.deleteMany({}) ;
    await Listing.insertMany(initData.data) ;
    console.log("Data is initalised") ;
};

initDB() ;

here is the listing schema

const listingSchema = new Schema({
  title: {
    type: String,
    required: true,
  },
  discription: String,
  image: {
    type: String,
    default:
      "https://plus.unsplash/premium_photo-1736880269895-84c3265dbfc3?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHw2OXx8fGVufDB8fHx8fA%3D%3D",
    set: (v) =>
      v === ""
        ? "https://plus.unsplash/premium_photo-1736880269895-84c3265dbfc3?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHw2OXx8fGVufDB8fHx8fA%3D%3D"
        : v,
  },
  price: Number,
  location: String,
  country: String,
});

here the error message

ValidationError: Listing validation failed: image: Cast to string failed for value "{
  filename: 'listingimage',
  url: 'https://images.unsplash/photo-1552733407-5d5c46c3bb3b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fHRyYXZlbHxlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=800&q=60'
}"

here the sample data i am trying to intialise

{
    title: "Secluded Beach House in Costa Rica",
    description:
      "Escape to a secluded beach house on the Pacific coast of Costa Rica. Surf, relax, and unwind.",
    image: {
      filename: "listingimage",
      url: "https://images.unsplash/photo-1499793983690-e29da59ef1c2?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8YmVhY2glMjBob3VzZXxlbnwwfHwwfHx8MA%3D%3D&auto=format&fit=crop&w=800&q=60",
    },
    price: 1800,
    location: "Costa Rica",
    country: "Costa Rica",
  },
Share Improve this question edited Jan 31 at 6:37 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 31 at 4:51 DarshanDarshan 11 bronze badge 1
  • Looks to me like you're simply trying to assign an object with filename and url properties into the image string field. Don't do that ¯\_(ツ)_/¯ – Phil Commented Jan 31 at 5:03
Add a comment  | 

1 Answer 1

Reset to default 1

You're trying to set an object like this...

{
  "filename": "listingimage",
  "url": "https://images.unsplash/photo-..."
}

into your String image field.

If you wanted to cater for this, you could use your setter to look out for such objects.

For example

const defaultImage =
  "https://plus.unsplash/premium_photo-1736880269895-84c3265dbfc3?w=500&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHw2OXx8fGVufDB8fHx8fA%3D%3D";

const listingSchema = new Schema({
  title: {
    type: String,
    required: true,
  },
  discription: String,
  image: {
    type: String,
    default: defaultImage,
    set: (v) => (v.url ?? v) || defaultImage,
  },
  price: Number,
  location: String,
  country: String,
});

Alternately, transform your input data to the correct format first, eg

Listing.insertMany(initData.data.map(({ image, ...l }) => ({
  ...l,
  image: image.url,
})));

本文标签: javascriptHow to initialise the data containing certain imagesStack Overflow