mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

CastError: Cast to Embedded failed for value

Open dmmulroy opened this issue 10 years ago • 6 comments

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?

dmmulroy avatar Oct 03 '16 15:10 dmmulroy

Can you show me what data you're passing in? Or, better yet, a more complete code sample?

vkarpov15 avatar Oct 07 '16 00:10 vkarpov15

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.

sandeepaboutclinic avatar May 09 '17 11:05 sandeepaboutclinic

Looks like the embedded value is a string not an object @sandeepaboutclinic . Check typeof doc.address before saving.

vkarpov15 avatar May 13 '17 01:05 vkarpov15

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

Berry06 avatar Jan 17 '18 23:01 Berry06

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 avatar Apr 10 '19 06:04 hirengevariya

@hirengevariya please don't double post

vkarpov15 avatar Apr 13 '19 15:04 vkarpov15