我正在使用mongoose(7.2.2)将对象保存到mongodb中。架构如下所示
// database schema
const schema = Schema({
"name":{type:String},
"description":{type:String},
"database":{
type:{type:String},
url:{ type:String },
db:{ type:String },
username:{ type:String },
password:{ type:String },
},
"schema":[ {type:Schema.Types.Mixed} ],
},{
timestamps: true,
virtuals: true,
toJSON: {
transform: (docs, ret) => {
const obj = ret;
obj.id = obj._id;
delete obj.__v;
return obj;
}
}
});
mongoose.model('data_assets',dataAssetsSchema);
// save the data
const collection = mongoose.model('data_assets');
return await collection.create(data);
// data example
{
schema: [
{
name: 'name',
column: 'name',
role: 'dimension',
type: 'string'
},
{ name: 'latitude', column: 'latitude', role: 'measure', type: 'float' },
{
name: 'longtitude',
column: 'longtitude',
role: 'measure',
type: 'float'
}
],
name: 'china_train_station',
database: {
url: 'mongodb://localhost:27017',
username: '',
password: '',
db: 'test',
collection: 'station',
indexing: 'name'
}
}
我没有看到代码有任何问题,但保存数据给我错误。
err TypeError: Cannot read properties of undefined (reading 'options')
at getEmbeddedDiscriminatorPath (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/helpers/document/getEmbeddedDiscriminatorPath.js:25:79)
at model.$set (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/document.js:1149:16)
at model.$set (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/document.js:1208:14)
at model.$set (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/document.js:1100:14)
at model.Document (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/document.js:165:12)
at model.Model (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/model.js:122:12)
at new model (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/model.js:4609:15)
at /Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/model.js:2853:16
at Array.map (<anonymous>)
at Function.create (/Users/Documents/code/data/server2/node_modules/_mongoose@7.2.2@mongoose/lib/model.js:2842:38)
以前从来没有遇到过问题,我很困惑
1条答案
按热度按时间bybem2ql1#
种子数据的
database
字段与database
字段架构不匹配。