CastError: Cast to Embedded failed for value
Upgrading to 4.6.2 is causing an error to be thrown:
CastError: Cast to Embedded failed for value "{ label: 'DEFAULT LABEL',
token: '',
_id: 'r1LxVo1gA' }" at path "childSchemaKey" at MongooseError.CastError
We have a schema that used the the above schema { label, token, id } as a child under they key 'childSchemaKey'. 4.6.1 and prior does not throw any errors.
Any idea what is causing this?
Can you show me what data you're passing in? Or, better yet, a more complete code sample?
hi expert i am getting error address": { "message": "Cast to Embedded failed for value "{ type: 'home', line1: 'A-10 37, Siddharth Nagar', line2: 'Goregaon West', city: '58cbd4e9c28602397fb1d887', pincode: '100444' }" at path "address"", "name": "CastError", "stringValue": ""{ type: 'home', line1: 'A-10 37, Siddharth Nagar', line2: 'Goregaon West', city: '58cbd4e9c28602397fb1d887', pincode: '100444' }"", "kind": "Embedded", "value": { "type": "home", "line1": "A-10 37, Siddharth Nagar", "line2": "Goregaon West", "city": "58cbd4e9c28602397fb1d887", "pincode": "100444" },
Saving data
"email": "[email protected]", "address": { "type":"home", "line1":"A-10 37, Siddharth Nagar", "line2":"Goregaon West", "city":"58cbd4e9c28602397fb1d887", "pincode":"100444"}
My schema is as
module.exports.addressSchema = function () {
return new mongoose.Schema({
type: String
, line1: String
, line2: String
, city: [{ type: Schema.Types.ObjectId, ref: 'cities'}]
, pincode: String
});
};
var PersonalDetailSchema = new mongoose.Schema({
email: {type: String, required: true},
address: common.addressSchema(),
});
please help me what could be the issue.
Looks like the embedded value is a string not an object @sandeepaboutclinic . Check typeof doc.address before saving.
Hi Expert,
I'm getting a similar error front side.
"Cast to embedded failed for value "{ media: { contentType: 'video/webm', videoData: {} },\n input: '',\n result: false }" at path "results""name"CastError"stringValue""{ media: { contentType: …'',\n result: false }""kind"embedded"value"{ media: { contentType: 'video/webm', videoData: {} }, input: '', result: false }"path"results"reasonObjectmessage"Cast to Buffer failed for value "{}" at path "media.videoData""name"CastError"stringValue""{}""kind"Buffer"valueObjectpath"media.videoData"reasonObject
Code server side : let arrayBufferNew = null; fileReader.onload = function(progressEvent) { arrayBufferNew = this.result; try { exercice.results[0].media.data = arrayBufferNew; } catch (error) { console.log('Ko!'); } }; fileReader.readAsArrayBuffer(blob);
My schema is : var Schema = mongoose.Schema;
var exerciceSchema = new Schema({ title: { type: String, Required: 'title mandatory' }, statement: { type: String }, category: { type: String }, results: [{ meta: Schema.Types.Mixed, media: { videoData: Buffer, contentType: String }, input: String, result: Boolean }], unitTest: [{ meta: Schema.Types.Mixed, input: String }] });
What is the problem with the buffer cast ? Thks
Hi, getting same issue with m schema
// Create Geo Location Schema const GeoSchema = new mongoose.Schema({ type: {type: String, default: 'Point'}, coordinates: {type: [Number], index: '2dsphere'} });
// Create Uer Location Schema const UserLocationSchema = new mongoose.Schema({ name: String, geometry: GeoSchema, createdAt: Date, updatedAt: Date });
Data Inserting.. newUserLocation: function (req, res) {
const {name, location} = req.body;
// Create user location data
const userLocationData = {
name,
location
};
console.log('New User Data', userLocationData);
UserModel.insert(userLocationData).then((userCreateRes) => {
res.json({status: true, data: userCreateRes, message: 'User location successfully added'})
}).catch(() => {
res.json({status: false, message: 'User location data failed!'})
})
},
message: 'Cast to Embedded failed for value "[-73.856077, 40.848447]" at path "location"', name: 'CastError', stringValue: '"[-73.856077, 40.848447]"', kind: 'Embedded', value: '[-73.856077, 40.848447]', path: 'location', reason: [MongooseError] } }, _message: 'userLocation validation failed', name: 'ValidationError' }
@hirengevariya please don't double post