首先,我是angular,mongodb和nodejs的新手。我开始开发一个简单的API来开始工作,但现在我只想完成个人学习。
它使用地理坐标存储位置,并且我尝试根据用户位置的最小和最大距离查询这些位置。但是,每当我尝试此查询时,我都会拒绝连接。我已经试图让它工作好几天了,但是无法弄清楚这个问题。
我的位置架构是
var LocationSchema = new Schema({
locationName: {
type: String,
required: true
},
description: {
type: String,
Required: true
},
geolocation: {
type: { type: mongoose.Schema.Types.Point, default:"Point" },
coordinates: [Number],
},
我将距离和用户位置作为参数发送:
exports.find_all_locations_near = function (req, res) {
const $minDistance = req.query.minDistance;
const $maxDistance = req.query.maxDistance;
lat = req.query.lat;
lon = req.query.lon;
if ($minDistance > 0) {
Location.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [lat, lon] },
distanceField: "dist.calculated",
minDistance: $minDistance,
maxDistance: $maxDistance,
query: { type: "public" },
includeLocs: "dist.location",
num: 5,
spherical: true
}
}
]).pretty(), function (err, location) {
if (err) {
//console.log(err);
res.send(err);
}
else if (location == '')
//res.json({ message: 'No location was found.' });
res.json({ message: 'No location was found.' });
else {
res.json(location);
res.json({ message: 'The end.' });
}
};
} else {
Location.find({}, function (err, location) {
if (err)
res.send(err);
res.json(location);
res.json(req.params.minDistance);
});
}
}
不确定问题出在哪里,所有其他查询和请求都有效。我路线上的东西?
app.route('/api/places')
.get(geolocation.find_all_locations_near);
//.get(geolocation.list_all_locations);
我得到的错误是:
获取http://localhost:3000/api/places?minDistance=10&maxDistance=100000000000&lat=45.2361152&lon=-66.1742988 net :: ERR_CONNECTION_REFUSED angular.min.js 100
我很高兴能够帮助你理解发生的事情。如果您需要更多信息,github为https://github.com/supermonteiro/geolocationAPI