I am trying to save latitude and longitude to mongoose model but I am unable to store them

883 views Asked by At

Here is my mongoose model

var mongoose = require('mongoose'),
Schema = mongoose.Schema

var DriverSchema = new mongoose.Schema({

    carNumber : {type : String, required : true, unique : true},

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

    loc : {
        type: [Number],
        index:'2d'
    }

});

I am trying to post the latitude and longitude from postman which is :

carNumber         KA51B7499
DriverName        Sachin
longitude         74.9999
latitude          67.2563

I am saving this request in mongoose in the following code :

router.post('/',function(req,res){

    var Driver = new trackCab({
        carNumber: req.body.carNumber,
        DriverName: req.body.DriverName,
        loc: {
            type: [parseFloat(req.body.longitude),parseFloat(req.body.latitude)]
        }
    });
    //console.log(req.body.longitude);
    //console.log(req.body.latitude);
    Driver.save(function(err){
        if(err) console.log(err);
        console.log('User saved successfully!');
    });
});

However longitudes and latitudes are not saved in loc object.

My returned object in postman looks like this :

 {
        "_id": "55773165dda03a9c14cb8468",
        "carNumber": "KA51B7499",
        "DriverName": "Jagdeesh",
        "__v": 0
    },
1

There are 1 answers

0
Muhammad Ali On

wrong object you are using , loc type is Array , not an Object

var Driver = new trackCab({
    carNumber: req.body.carNumber,
    DriverName: req.body.DriverName,
    loc:  [parseFloat(req.body.longitude),parseFloat(req.body.latitude)]
    })