Search code examples
mongodbmongooseschemageojsonopenlayers-3

GeoJSON data saving but returns undefined when queried


I have the following schema

    var RaceSchema = new Schema({
    account: {
        _id: String,
    name: String,
    country: String
    },
    raceName:  String,
    raceStartTime: Date,
    timezone: Number,
    trackTimeStart: Date,
    trackTimeFinish: Date,
    sponserImageURL: Array,     
    map: {
        center: Array,
        course: Object,
        zoomLevel: Number
    },
    boats: [{
            _id: String,
            handicaps: [{                   
                                name: String,
                    division: Number,
                    rating: Number,
                    correctedTime: Number,
                    place: Number
                            }]
    }],
    dtg: Number,
    eta: Date,
    finishTime: Date,
    socialMedia: [{
                    name: String,
                    link: String
    }],
    createdAt: { type: Date, default: new Date },
    updatedAt: Date
}, {
    collection: 'race'
});

Which I am using to store GeoJson data along with some other information as seen in the schema. I am saving the data in the following format (its the response that i get from the server on saving)

{
    "_id": "562bc48bfde8999f2995e61d",
    "raceStartTime": "2015-10-29T17:48:00.000Z",
    "trackTimeStart": "2015-10-24T17:48:00.000Z",
    "trackTimeFinish": "2015-10-24T17:48:00.000Z",
    "raceName": "Test ",
    "timezone": -4.5,
    "__v": 1,
    "updatedAt": "2015-10-24T17:49:16.912Z",
    "createdAt": "2015-10-24T17:39:45.587Z",
    "socialMedia": [],
    "boats": [{
        "_id": "55f6c2d39a2496d01d000002",
        "handicaps": []
    }],
    "map": {
        "center": [78.4651562, 17.4079808],
        "course": "{\"type\":\"LineString\",\"coordinates\":[[8639830.18113004,2037505.4259696582],[8738281.073561348,1972075.3297575472],[8619650.805662755,1928659.0976915671],[8645333.647166574,1981859.2693780498],[8598248.437742906,2021606.5240863415]]}",
        "zoomLevel": 8
    },
    "sponserImageURL": [],
    "account": {
        "_id": "55a7c944d65763c23d11dd96",
        "name": "Huy Tran"
    },
    "id": "562bc48bfde8999f2995e61d"
}

I am saving the data for map.course as an object as seen from my schema in the same format that i receive from open layers api. This I am guessing is causing my findByIdAndRemove(id, callback); to return a

TypeError: Cannot read property '562bc2b5fde8999f2995e61c' of undefined

How can I fix this issue so that I can perform other operations other than save on this schema


Solution

  • You seem to be saving map.course as a String (of JSON data), not as an Object.

    If that is GeoJSON created by ol.format.GeoJSON, consider using writeFeatureObject instead of writeFeature. They return the same result, but the first is an actual JS object and the seconds is a String with JSON.