英文:
can't save data in mongoose 7.2.2 with error Cannot read properties of undefined (reading 'options') at getEmbeddedDiscriminatorPath
问题
我正在使用mongoose(7.2.2)将一个对象保存到mongodb中。模式如下所示:
// 数据库模式
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);
// 保存数据
const collection = mongoose.model('data_assets');
return await collection.create(data);
// 数据示例
{
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)
之前从未遇到过这个问题,我很困惑。
英文:
I am working with mongoose (7.2.2) to save an object into mongodb. The schema is shown below
// 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'
}
}
I see nothing wrong with the code but saving the data gives me the error.
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)
Never encounter the problem before, I am very confused
答案1
得分: 1
const dataAssetsSchema = Schema({
name: String,
database: {
type: { type: String },
url: { type: String },
db: { type: String },
username: { type: String },
password: { type: String },
collection: String,
indexing: String,
},
schema: [{ type: Schema.Types.Mixed }],
})
// 种子数据
const data = {
schema: [
{
name: 'name',
column: 'name',
role: 'dimension',
type: 'string',
},
],
name: 'china_train_station',
database: {
type: 'NOSQL',
url: 'mongodb://localhost:27017',
username: '',
password: '',
db: 'test',
collection: 'station',
indexing: 'name',
},
};
英文:
"mongoose": "^7.2.1"
The database
field of the seed data does not match the database
field schema.
const dataAssetsSchema = Schema({
name: String,
database: {
type: { type: String },
url: { type: String },
db: { type: String },
username: { type: String },
password: { type: String },
collection: String,
indexing: String,
},
schema: [{ type: Schema.Types.Mixed }],
})
// seed data
const data = {
schema: [
{
name: 'name',
column: 'name',
role: 'dimension',
type: 'string',
},
],
name: 'china_train_station',
database: {
type: 'NOSQL',
url: 'mongodb://localhost:27017',
username: '',
password: '',
db: 'test',
collection: 'station',
indexing: 'name',
},
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论