location object expected, location array not in correct format

折月煮酒 提交于 2019-12-12 08:06:51

问题


I have spent doing such a straight forward thing. I just want to do a CRUD operation on a user model using nodejs, mongoose, restify stack. My mongo instance is on mongolab. The user should contain a "loc" field . User schema is as follows :

var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var userSchema = new Schema( {
email_id : { type: String,  unique: true },
password: { type: String},
first_name: String,
last_name: String,
age: String,
phone_number: String,
profile_picture: String,
loc: {
     type: {},
    coordinates: [Number]
}  
});
userSchema.index({loc:'2d'});
var User = mongoose.model('user', userSchema);
module.exports = User;

the rest api used to post is as follows :

create_user : function (req, res, next) {
var coords = [];
coords[0] = req.query.longitude;
coords[1] = req.query.latitude;

var user = new User(
    {
        email_id : req.params.email_id,
        password: req.params.password,
        first_name: req.params.first_name,
        last_name: req.params.last_name,
        age: req.params.age,
        phone_number: req.params.phone_number,
        profile_picture: req.params.profile_picture,
        loc: {
                type:"Point",
                coordinates: [1.0,2.0] // hardcoded just for demo
            }
    }
    ); 
user.save(function(err){ 
    if (err) { 
        res.send({'error' : err}); 
    }
        res.send(user);
    });  
return next();
},

Now when i do a POST call on curl -X POST http://localhost:3000/user --data "email_id=sdass@dfAadsfds&last_name=dass&age=28&phone_number=123456789&profile_picture=www.jakljf.com&longitude=1.0&latitude=2.0" I get the following error

{
error: {
code: 16804
index: 0
errmsg: "insertDocument :: caused by :: 16804 location object expected,           location array not in correct format"
op: {
email_id: "sdass@dfAadsfdsadkjhfasvadsS.com"
password: "sdass123DadakjhdfsfadfSF45"
first_name: "shaun"
last_name: "dass"
age: "28"
phone_number: "123456789"
profile_picture: "www.jakljf.com"
loc: {
coordinates: [2]
0:  1
1:  2
-
type: "Point"
}-
_id: "55efc95e0e4556191cd36e5e"
__v: 0
}-
}-    
}

The location field is giving problems as the POST call works just fine if i remove the loc field from model

Below are the hits/trials I did : 1) Change userSchema.index({loc:'2d'}); to userSchema.index({loc:'2dsphere'}); 2) Changing loc schema to everything given in Stackoverflow. I would like to know the right way to define this though. 3) Passing the hardcode 2d array but still it says Location object expected, location array not in correct format" what format is required for this ?

Any help in this regard is greatly appreciated. Thanks.


回答1:


MongoDB 2d index requires the legacy coordinates pairs format, which is just an array of coordinates like [1, 2].

If you need GeoJSON support, please use the 2dsphere index.

userSchema.index({loc:'2dsphere'});


来源:https://stackoverflow.com/questions/32472023/location-object-expected-location-array-not-in-correct-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!