【发布时间】:2015-02-03 04:22:52
【问题描述】:
我收到 ObjectId 的转换错误,我真的不明白为什么:
CastError: Cast to ObjectId failed for value "[object Object]" at path "states"
var data = require('../../../data/seed/refs/countries/countries.json'); model.Super_CountryRefs.find({}).exec(function (err, collection) { if (collection.length === 0) { data.forEach(function (country) { var countryModel = new model.Super_CountryRefs; var country_id = mongoose.Types.ObjectId(); countryModel._id = country_id; countryModel.name = country.name; countryModel.abbr = country.abbr; countryModel.code = country.code; countryModel.flag = country.flag; if(country.states != undefined){ if(country.states.length > 0){ country.states.forEach(function(state){ console.log('State: ' + state.name) var stateModel = new model.Super_StateProvinceRefs; stateModel.name = state.name; stateModel.abbr = state.abbr; EXCEPTION BEING THROWN HERE --> stateModel.country = countryModel._id; stateModel.save(function(err){ if(err){ console.log('Error: ' + err) } else { countryModel.states.push(state) } }) }) } } countryModel.save(function(err){ if(err) console.log('Error: ' + err); }) }) console.log('Country/State Seed Complete'); } });
我的国家模型:
country: {
type: Schema.ObjectId,
ref: 'SuperCountry',
required: 'Country is required'
}
我的国家模型:
states : [{
type: Schema.ObjectId,
ref: 'SuperStateProvinceRefs'
}]
为了让事情变得更奇怪,数据被正确地填充到了数据库中。谁能看到我为什么会收到错误?
【问题讨论】:
标签: mongoose