当我尝试在MapBox GL JS地图上显示地图标记时,我的Angular前端在向我的节点/Mongo后端发出请求后返回一个AssertionError:
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路由:
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
}
}Successful response using REST client
Working result with the $geoNear expression removed from find()
发布于 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=');
这解决了我的问题。
https://stackoverflow.com/questions/55821279
复制相似问题