Mongoose: Cast to String failed for value "[object Object]" at path "data"

Created on 25 May 2016  路  2Comments  路  Source: Automattic/mongoose

os: window7 x64
mongodb: 3.2.1
mongoose: 4.4.19

var b = new Schema({
    date: Date,
    partId: String,
    data: {
        name: String,
        type: String,
        partId: String,
        total: Number,
        value: Number
    }
}, {
    collection: 'blood',
    autoIndex: false
});

with b.create method, I got the exception: Cast to String failed for value "[object Object]" at path "data" .

if I change the schema to

new Schema({
    date: Date,
    partId: String,
    data: {}
}, {
    collection: 'blood',
    autoIndex: false
})

it just works fine.

what's the problem?

help

Most helpful comment

var b = new Schema({
    date: Date,
    partId: String,
    data: {
        name: String,
        type: String,
        partId: String,
        total: Number,
        value: Number
    }
}

Because data has a 'type' property, mongoose interprets your schema as having a property 'data' of type 'String'. See http://mongoosejs.com/docs/guide.html#typeKey

All 2 comments

var b = new Schema({
    date: Date,
    partId: String,
    data: {
        name: String,
        type: String,
        partId: String,
        total: Number,
        value: Number
    }
}

Because data has a 'type' property, mongoose interprets your schema as having a property 'data' of type 'String'. See http://mongoosejs.com/docs/guide.html#typeKey

谋 didnt add schma but i see same error

var mongoose=require('mongoose');

mongoose.Promise=require('bluebird');

var mongoDB='mongodb://localhost/NodeProje';

mongoose.connect(mongoDB, function(err,err){
if(err)
console.log('mongoose error:' + err);
else
console.log('mongoose connect:' + mongoDB);

});

Was this page helpful?
0 / 5 - 0 ratings