Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'm currently having problems in creating a schema for the document below. The response from the server always returns the "trk" field values as [Object]. Somehow I have no idea how this should work, as I tried at least all approaches which made sense to me ;-)

If this helps, my Mongoose version is 3.6.20 and MongoDB 2.4.7 And before I forget, it would be nice to also set it as Index (2d)

Original data:

"_id": ObjectId("51ec4ac3eb7f7c701b000000"), 

"gpx": { 

"metadata": { 

"desc": "Nürburgring VLN-Variante", 

"country": "de", 

"isActive": true 

}, 

"trk": [ 

"lat": 50.3299594, 

"lng": 6.9393006 

}, 

"lat": 50.3295046, 

"lng": 6.9390688 

}, 

"lat": 50.3293714, 

"lng": 6.9389939 

}, 

"lat": 50.3293284, 

"lng": 6.9389634 

}] 

}

Mongoose Schema:

var TrackSchema = Schema({ 

_id: Schema.ObjectId, 

gpx: { 

metadata: { 

desc: String, 

country: String, 

isActive: Boolean 

}, 

trk: [{lat:Number, lng:Number}] 

}, { collection: "tracks" });

The response from the Network tab in Chrome always looks like this (that's only the trk-part which is wrong) :

{ trk: 

[ [Object], 

  [Object], 

  [Object], 

  [Object], 

  [Object], 

  [Object],

I already tried different Schema definitions for "trk":

  1. trk: Schema.Types.Mixed

  2. trk: [Schema.Types.Mixed]

  3. trk:[ { type:[Number], index: "2d" }]

Hope you can help me ;-)

2 Answers

0 votes
by (106k points)

If you want to define an object in array in Mongoose schema correctly with 2d geo index then you can declare trk by the following ways: -

trk : [{ 

lat : String, 

lng : String 

}]

0 votes
by (108k points)

When you were sending the response, you were converting it to a string via .toString().

To avoid that action, you have to change the parameter to :

fields: [

    {

      name: String,

      type: {

        type: { type: String }

      },

      registrationEnabled: Boolean,

      checkinEnabled: Boolean

    }

  ]

Related questions

0 votes
2 answers
0 votes
2 answers
0 votes
2 answers
0 votes
1 answer
asked Jan 5, 2020 in Web Technology by ashely (50.2k points)

Browse Categories

...