NodeJS 无法在mongoose 7.2.2中保存数据,错误为无法读取getEmbeddedDiscriminatorPath中未定义的属性(阅读“选项”)

v2g6jxz6  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(98)

我正在使用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)

以前从来没有遇到过问题,我很困惑

bybem2ql

bybem2ql1#

  • “ Mongoose ”:“^7.2.1”*

种子数据的database字段与database字段架构不匹配。

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',
  },
};

相关问题