首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB GeoJSON $near AssertionError

MongoDB GeoJSON $near AssertionError
EN

Stack Overflow用户
提问于 2019-04-24 09:50:43
回答 1查看 80关注 0票数 0

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

代码语言:javascript
复制
actual: false
code: "ERR_ASSERTION"
expected: true
generatedMessage: true
name: "AssertionError [ERR_ASSERTION]"
operator: "=="

这是一个使用Node/MongoDB后端的Angular 7应用程序。我有一个具有GeoJSON points的"Users“集合。我尝试在$near$geoNear中使用db.collection.find()方法,这两种方法都会产生错误。如果我删除这些表达式并执行一个简单的db.collection.find({}),响应将按预期返回。

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

我也尽我所能在谷歌和Stackoverflow上寻找了一个无用的答案。

users.js路由:

代码语言:javascript
复制
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():

代码语言:javascript
复制
  getMarkers(): Observable<GeoJson> {
    return this.http.get<any>('http://localhost:3000/users');
  }

GeoJson类:

代码语言:javascript
复制
export class GeoJson implements IGeoJson {
  type = 'Feature';
  geometry: IGeometry;

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

map.component:

代码语言:javascript
复制
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({})将返回我的所有文档,然后我可以映射。问题似乎出在:

代码语言:javascript
复制
'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()

EN

回答 1

Stack Overflow用户

发布于 2019-04-26 02:03:59

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

return this.http.get<any>('http://localhost:3000/users');

应该是:

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

这解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55821279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档