can't save data in mongoose 7.2.2 with error Cannot read properties of undefined (reading 'options') at getEmbeddedDiscriminatorPath

huangapple go评论71阅读模式
英文:

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({
&quot;name&quot;:{type:String},
&quot;description&quot;:{type:String},
&quot;database&quot;:{ 
type:{type:String},
url:{ type:String },
db:{ type:String },
username:{ type:String },
password:{ type:String },
},
&quot;schema&quot;:[ {type:Schema.Types.Mixed} ],
},{
timestamps: true, 
virtuals: true,
toJSON: {
transform: (docs, ret) =&gt; {
const obj = ret;
obj.id = obj._id;
delete obj.__v;
return obj;
}
}
});
mongoose.model(&#39;data_assets&#39;,dataAssetsSchema);
// save the data
const collection = mongoose.model(&#39;data_assets&#39;);
return await collection.create(data);
// data example
{
schema: [
{
name: &#39;name&#39;,
column: &#39;name&#39;,
role: &#39;dimension&#39;,
type: &#39;string&#39;
},
{ name: &#39;latitude&#39;, column: &#39;latitude&#39;, role: &#39;measure&#39;, type: &#39;float&#39; },
{
name: &#39;longtitude&#39;,
column: &#39;longtitude&#39;,
role: &#39;measure&#39;,
type: &#39;float&#39;
}
],
name: &#39;china_train_station&#39;,
database: {
url: &#39;mongodb://localhost:27017&#39;,
username: &#39;&#39;,
password: &#39;&#39;,
db: &#39;test&#39;,
collection: &#39;station&#39;,
indexing: &#39;name&#39;
}
}

I see nothing wrong with the code but saving the data gives me the error.

err TypeError: Cannot read properties of undefined (reading &#39;options&#39;)
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 (&lt;anonymous&gt;)
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: &#39;name&#39;,
      column: &#39;name&#39;,
      role: &#39;dimension&#39;,
      type: &#39;string&#39;,
    },
  ],
  name: &#39;china_train_station&#39;,
  database: {
    type: &#39;NOSQL&#39;,
    url: &#39;mongodb://localhost:27017&#39;,
    username: &#39;&#39;,
    password: &#39;&#39;,
    db: &#39;test&#39;,
    collection: &#39;station&#39;,
    indexing: &#39;name&#39;,
  },
};

huangapple
  • 本文由 发表于 2023年6月19日 03:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定