【问题标题】:MongoDB GeoJSON $near AssertionErrorMongoDB GeoJSON $near AssertionError
【发布时间】:2019-04-26 02:03:59
【问题描述】:

尝试在 MapBox GL JS 地图上显示地图标记时,我的 Angular 前端在向我的 Node/Mongo 后端发出请求后返回 AssertionError

actual: false
code: "ERR_ASSERTION"
expected: true
generatedMessage: true
name: "AssertionError [ERR_ASSERTION]"
operator: "=="

这适用于使用 Node/MongoDB 后端的 Angular 7 应用程序。我有一组带有 GeoJSON 点的“用户”。我尝试将db.collection.find() 方法与$near$geoNear 一起使用,这两种方法都会产生错误。如果我删除这些表达式并执行简单的db.collection.find({}),则响应会按预期返回。

另外,我可以使用带有$geoNear express 的 REST 客户端(Chrome 的 Restlet)发出请求,并且结果正确返回。

我也尽我所能在 Google 和 Stackoverflow 上寻找答案,但无济于事。

users.js 路由:

router.get('/trucks', (req, res, next) => {
    User
    .find({
        'geometry': {
            $nearSphere: {
                $geometry: {
                    type : "Point",
                    coordinates: [
                        parseFloat(req.query.lng),
                        parseFloat(req.query.lat),
                    ]
                },
                $maxDistance : 100000
            }
        }
    })
    .then((users, err) => {
        if (err) res.json({success: false, message: 'There was a problem with the lookup.'});
        if (!users) res.json({success: true, message: "Sorry, we couldn't find anyone in your area."})
        let results = users.map(user=> {
            let userResult = {
                id: user._id,
                name: user.name,
                username: user.username,
                email: user.email,
                geometry: {
                    type: user.geometry.type,
                    coordindates: user.geometry.coordinates
                }
            }
            return userResult;
        });

        res.json({
            success: true,
            users: results
        });
    })
    .catch(err =>
        res.json(err)
    );
})

map.service.getMarkers():

  getMarkers(): Observable<GeoJson> {
    return this.http.get<any>('http://localhost:3000/users');
  }

GeoJson 类:

export class GeoJson implements IGeoJson {
  type = 'Feature';
  geometry: IGeometry;

  constructor(coordinates, public properties?) {
    this.geometry = {
      type: 'Point',
      coordinates: coordinates
    }
  }
}

map.component:

ngOnInit() {
    this.markers = this.mapService.getMarkers();
    this.initMap();
  }

...

private initMap() {
    // ommited code to get location using navigator geolocation API

    this.setMap()
  }

...

setMap() {
    // ommited code to style map

      this.markers.subscribe((result) => {
        let markers = [];

        result.trucks.forEach(truck => {
           console.log(user.geometry.coordindates);
           let coordinates = user.geometry.coordindates;
           let newMarker = new GeoJson(coordinates, { message: user.name });
           markers.push(newMarker)
        })

        let data = new FeatureCollection(markers)
        this.source.setData(data)
      })

    })
}

我希望收到包含我的用户的响应,以便我可以映射他们的位置。就像我上面说的,我可以使用 REST 客户端发出请求,一切都按预期工作。此外,通用db.collection.find({}) 将返回我的所有文档,然后我可以映射。问题似乎出在:

'geometry': {
            $nearSphere: {
                $geometry: {
                    type : "Point",
                    coordinates: [
                        parseFloat(req.query.lng),
                        parseFloat(req.query.lat),
                    ]
                },
                $maxDistance : 100000
            }
        }

Sample document from MongoDB

Successful response using REST client

Working result with the $geoNear expression removed from find()

【问题讨论】:

  • 这不是 MongoDB 错误,但似乎是来自已实现的 Mapbox 函数,您实际上并未在此处的代码中显示任何用法。据我所知,直到 setMap() 方法的所有内容都应该可以正常工作。因此,错误可能会在处理结果的代码中产生数据被检索到客户端之后。请注意,subscribe() 调用确实属于初始化函数,而不是重复调用。
  • 感谢您的回复。我忽略的非常简单的修复。

标签: node.js angular mongodb express geojson


【解决方案1】:

我完全忽略了一个事实,即我没有将任何坐标与请求一起发送到我的 Node 后端:

return this.http.get&lt;any&gt;('http://localhost:3000/users');

应该是:

return this.http.get&lt;any&gt;('http://localhost:3000/users?lng=&amp;lat=');

这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-03-06
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 2015-05-28
    • 2012-11-29
    相关资源
    最近更新 更多