Mongoose - 路径 "states" 处的值“[object Object]”的 ObjectId 失败

Mongoose - ObjectId failed for value "[object Object]" at path "states"

我收到 ObjectId 的转换错误,我真的不明白为什么:

CastError:为路径 "states"[=15= 处的值“[object Object]”转换为 ObjectId 失败]

  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'
}]

更奇怪的是,数据已正确填充到数据库中。谁能看出我为什么会收到错误消息?

实际上这不是错误:

    stateModel.country = countryModel._id;

这是错误:

   countryModel.states.push(state)

当国家/地区的模式具有状态的 ObjectId 引用时,我正在推送整个状态对象。

这是正确答案:

    countryModel.states.push(state._id)