Mongoose GeoJSON在使用中间件时抛出“ MongoError:无法提取地理键”

问题描述 投票:0回答:1

我在NodeJS项目中使用Mongoose,并且在子文档数组中有一个GeoJSON对象:

ParentSchema.js

import mongoose from 'mongoose';
import ChildeSchema from '[path]/ChildSchema.js';

const ParentSchema = mongoose.Schema({
  ...
  children: [ChildSchema],
  ...
});

ChildSchema.js

import mongoose from 'mongoose';
import geocoder from '[path]/geocoder.js'; // where I initialize the geocoder

const ChildSchema = mongoose.Schema({
  ...
  location: {    
    // GeoJSON
    type: {
      type: String,
      enum: ['Point'],
      default: 'Point'
    },
    // first enter longitude, then latitude
    coordinates: {
      type: [Number]
    //index: '2dsphere'  <-- committed out, also tried with the index
    }
  },
  ...
});

// middleware goes here

export default ChildSchema;

[我在ChildSchema中添加了一个中间件,该中间件使用与mapquest一起使用的geocoder获取位置信息。

中间件(在ChildSchema.js中:: >>

ChildScheme.pre('save', async function (next) {

  const geoDetails = await geocoder.geocode(this.fullAddress); // works fine, I'm getting the location

  // values are legal
  const latitude = geoDetails[0].latitude;
  const longitude = geoDetails[0].longitude;

  this.location = {
      type: 'Point',
      coordinates: [longitude, latitude] 
    };

  next();
}

当我尝试创建新的父文档(将POST与application / json Content-Type结合使用时,出现以下错误:

MongoError:无法提取地理键:{{_ parent_document}

地理元素必须是数组或对象:类型:\“ Point \”

我尝试应用在以下问题中找到的答案,但没有帮助:mongoose geojson in schema, “Can't extract geo keys” errorMongoError: Can't extract geo keysMongoose Schema for geoJson coordinatesHow does one represent MongoDB GeoJSON fields in a Mongoose Schema?Can't extract geo keys, unknown GeoJSON type: { coordinates: [ 13.42493130000003, 52.50074619999999 ]

我想念的是什么?预先感谢您的答复!

我在NodeJS项目中使用Mongoose,并且在子文档数组中有一个GeoJSON对象:ParentSchema.js从'mongoose'导入mongoose;从'[path] /ChildSchema.js'导入ChildeSchema; const ...

javascript node.js mongodb mongoose geojson
1个回答
0
投票

将密钥的名称更改为location以外的其他名称,并且它起作用了...

© www.soinside.com 2019 - 2024. All rights reserved.