Tuesday, 18 April 2017

Node.js code to do geo location search on Heroku

I have a question that I hope someone can help me with. I am trying to create an app that will use a mongodb to perform a search of people in a radius from a point. I have done this on a local mongodb and it works but when I try the same thing using node.js and going to Heroku it does not work. I can post each of the sample data into the database with no issue. I can see the index is created on the database. I can do the find based on the name. I just can not do the $near for some reason. Here is what I am trying to do:

Sample data:

{
  "name":"Frank",
  "loc": {"lon": 51, "lat": -114}
}

{
  "name":"Jim",
  "loc": {"lon": 151, "lat": 114}
}

In my Server.JS I am using:

var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var GEO_LOC = "geoloc";

var app = express();
app.use(bodyParser.json());

// Create a database variable outside of the database connection callback to reuse the connection pool in the app.
var db;

//Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
    if (err) {
        console.log(err);
        process.exit(1);
    }

    //Save database object from the callback for reuse.
    db = database;
    console.log("Database connection ready");

    //Initialize the app.
    var server = app.listen(process.env.PORT || 8080, function () {
        var port = server.address().port;
        console.log("App now running on port", port);
    });
});

//API ROUTES BELOW
// Generic error handler use by all endpoints.
function handleError(res, reason, message, code) {
    console.log("ERROR: " + reason);
    res.status(code || 500).json({"error": message});
}

app.post("/api/setgeo", function(req, res) {
    var newRL = req.body;

    db.collection(GEO_LOC).insertOne(newRL, function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to create.");
        } else {
            res.status(201).json(doc.ops[0]);
        }
    });

    db.collection(GEO_LOC).ensureIndex({"loc": "2d"});
});

app.get("/api/getname", function(req, res) {    
    var findQuery = "{";

    if (req.query.name) {
        findQuery = findQuery + " \"name\": \"" + req.query.name + "\"}";
    }

    db.collection(RESPONDER_LOCATION_COLLECTION).find(JSON.parse(findQuery)).toArray(function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to get information");
        } else {
            res.status(200).json(doc);
        }
    });
});

app.get("/api/getgeo", function(req, res) { 
    var findQuery = "{\"loc\": {$near:[51, -113]";}}";

    db.collection(RESPONDER_LOCATION_COLLECTION).find(JSON.parse(findQuery)).toArray(function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to get information");
        } else {
            res.status(200).json(doc);
        }
    });
});



via Justin Yanta

No comments:

Post a Comment