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
|
1 Answer
Reset to default 1You'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
版权声明:本文标题:javascript - How to initialise the data containing certain images? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741925878a2405282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
filename
andurl
properties into theimage
string field. Don't do that ¯\_(ツ)_/¯ – Phil Commented Jan 31 at 5:03