猫鼬geoNear返回空对象

时间:2019-04-05 15:22:25

标签: node.js mongoose

我正在尝试使用猫鼬geoNear功能,但是它一直向我发送空数组。 下面是我的代码。

我在猫鼬文档中找不到任何示例,我以前从未使用过此功能,仅遵循了教程,而且我不知道自己在做什么错。 如果有人可以帮助我或为我指明方向,将不胜感激。

预先感谢

pharmacieRoute.js:

/**
 * search pharmacies around latt & long point, based on perimeter limit
 * GET request
 * @QueryParam latt : lattitude
 * @QueryParam long: longitude
 * @QueryParam perimeter: limit perimeter of the research
 * @Return JSON array of pharmacies around
 */
router.get("/localisation", (req, res) => {

    var long = parseFloat(req.query.long);
    var latt = parseFloat(req.query.latt);
    var perimeter = req.query.perim;

    pharmacie.locatePharmacie(long, latt, perimeter, res);

 });

pharmacieController.js:

/**
 * query pharmacies around latt & long point, based on perimeter limit
 * @latt latt : lattitude
 * @long long: longitude
 * @perimter perimeter: limit perimeter of the research
 * @Return JSON array (pharmacies around)
 */
locatePharmacie : (long, latt, perimeter, res) => {

    var point = {
        type: "Point", 
        coordinates: [long, latt]
    }

    var geoOptions = {
        limit: perimeter, 
        spherical: true //spherical: based on earth round
    }

    try {

        Pharmacie.geoNear(point, geoOptions, (err, result, stats) => {

            if(err){

                res.status(500, contentTypeJson).send(err);

            }else if (result){

                res.status(200, contentTypeJson).send(result);

            } else if( stats ){

                res.send(stats);

            }
        });

    } catch (error) {

        res.send(error);
    }

}

PharmacieModel.js:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Product = require('./productModel');
const Coordinates = require('./coordinatesModel');

const coordinatesSchema = new Schema({

type : { //type of coordinate on the map (we are using geoJson, check geojson.org)
    type: String,  //type of data
    default: "Point"
},
coordinates : {
    type: [Number],//always set up this way : [long, latt]
    index: "2dsphere" //type of point mapping (we are using geoJson, check geojson.org)
    }
});

const pharmacieSchema = new Schema({

name: {type : String, required: true},

adress: {
    nbr:Number,
    street: String,
    zipCode: Number,
    city: String
},

turnover: Number,
trainingNeed: String,
productBought: [Product],
pharmacieCoordinates : coordinatesSchema

});

const Pharmacie = mongoose.model('pharmacie', pharmacieSchema);

module.exports = Pharmacie;

0 个答案:

没有答案