Geojson的Mongoose子文档

时间:2019-07-03 07:03:26

标签: node.js database mongodb mongoose

我无法定义正确的可重用点模式。我只是将示例架构复制到https://mongoosejs.com/docs/geojson.html

这是我在启动node.js应用程序时遇到的错误

/ home / ****** / projects / realista-api / node_modules / mongoose / lib / schema.js:418       抛出新的TypeError('模式路径' + prefix + key + '的无效值');             ^ TypeError:架构路径coordinates

的值无效

我已经尝试使用不可重用的架构。通过直接在父模式下定义它,就可以了

coordinates: {
    type: {
      type: String,
      enum: ['Point'],
      required: true
    },
    coordinates: {
      type: [Number],
      required: true
    }
  },

这是代码

import { Schema, Document } from 'mongoose';

interface Point extends Document {
  type: string,
  coordinates: Array<number>,
}

const PointSchema: Schema = new Schema({
  type: {
    type: String,
    enum: ['Point'],
    required: true
  },
  coordinates: {
    type: [Number],
    required: true
  }
}, {
  id: false
});

export {
  Point,
  PointSchema,
}

我正在将其用作另一个架构中的子文档

const ProjectSchema: Schema = new Schema({
  owner: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: false,
  },
  logo: {
    type: String,
    required: false,
  },
  name: {
    type: String,
    required: true,
  },
  location: {
    type: String,
    required: false,
  },
  suburb: {
    type: String,
    required: false,
  },
  stateCode: {
    type: String,
    required: false,
  },
  country: {
    type: String,
    required: false,
  },
  countryName: {
    type: String,
    required: false,
    unique: true,
    sparse: true,
  },
  coordinates: PointSchema,// this is the field in question
  commission: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  tax: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  propertyType: {
    type: Schema.Types.ObjectId,
    ref: 'PropertyType',
    required: true,
  },
  address: {
    type: String,
    required: false,
  },
  title: {
    type: String,
    required: false,
  },
  description: {
    type: String,
    required: false,
  },
  videoTour: {
    type: String,
    required: false,
  },
  matterPortUrl: {
    type: String,
    required: false,
  },
  currency: {
    type: String,
    required: false,
  },
  minPrice: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  maxPrice: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  otherPrice: {
    type: String,
    required: false,
  },
  featureLandSizeMin: {
    type: String,
    required: false,
  },
  featureLandSizeMax: {
    type: String,
    required: false,
  },
  featureLandSizeUnit: {
    type: String,
    required: false,
  },
  featureBuiltStart: {
    type: Date,
    required: false,
  },
  featureBuiltEnd: {
    type: Date,
    required: false,
  },
  featureNumOfLevel: {
    type: Number,
    required: false,
  },
  featureNumOfUnit: {
    type: Number,
    required: false,
  },
  featureFlooring: {
    type: String,
    required: false,
  },
  featureExterior: {
    type: String,
    required: false,
  },
  featureConcierge: {
    type: String,
    required: false,
  },
  indoorFeatures: {
    type: String,
    required: false,
  },
  outdoorFeatures: {
    type: String,
    required: false,
  },
  minBedrooms: {
    type: Number,
    required: false,
  },
  maxBedrooms: {
    type: Number,
    required: false,
  },
  minBathrooms: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  maxBathrooms: {
    type: Schema.Types.Decimal128,
    required: false,
  },
  minParking: {
    type: Number,
    required: false,
  },
  maxParking: {
    type: Number,
    required: false,
  },
  csvVariationPending: {
    type: Boolean,
    required: false,
    default: false,
  },
  isPrivate: {
    type: Boolean,
    required: false,
    default: false,
  },
  status: {
    type: Boolean,
    required: false,
    default: false,
  },
  variations: [{
    type: Schema.Types.ObjectId,
    ref: 'ProjectVariation',
  }],
  deletedAt: {
    type: Date,
    required: false,
  }
}, {
  collection: 'projects',
  timestamps: true,
  strict: false,
});

我在做什么错?预先感谢。

1 个答案:

答案 0 :(得分:0)

能够使其工作。希望这对使用node.js开发的其他人有所帮助

该问题是由两件事引起的:

  1. 在声明子文档(嵌套对象或对象数组)时,Mongoose在文档中存在type字段时会感到困惑,因为在Mongoose概念中,它是用于声明字段类型的保留字。在我的情况下,type键来自GeoJSON,因为MongoDB要求它采用这种格式。这是猫鼬docs的链接,以使您更好地理解。

我刚才所做的就是将PointSchema更改为此

import { Schema, Document } from 'mongoose';

interface Point extends Document {
  type: string,
  coordinates: Array<number>,
}

const PointSchema: Schema = new Schema({
  type: {
    $type: String,
    enum: ['Point'],
    required: true
  },
  coordinates: {
    $type: [Number],
    required: true
  }
}, {
  _id: false,
  typeKey: '$type',
});

export {
  Point,
  PointSchema,
}
  1. 在导入/请求PointSchema时,node.js中也存在循环依赖问题。这个错误
/home/******/projects/realista-api/node_modules/mongoose/lib/schema.js:418 throw new TypeError('Invalid value for schema path ' + prefix + key + ''); ^ TypeError: Invalid value for schema path coordinates

发生这种情况是因为在ProjectSchema中使用PointSchema时未定义它。

W / c证明了为什么Mongoose Github中的问题通常会在遇到该错误时指出,原因是拼写错误的类型(ObjectID而不是ObjectId),或者在我的情况下,未定义的w / c是无效类型。