node.js - Javascript objects - feature missing from overall object, yet exists -
strange problem here. i'm running node/express/mongoose/leaflet. pull array of locations db, , once callback initiated, iterate on locations find bunch of string passages deals each location. try append array of passages each location object, , append locations array geojson featurecollection.
location.find({}, { _id: 0 }, function (err, locations) { if (err) { console.log('db error loading locations'); res.redirect('/'); } else { var num = 0; console.log("beginning finding passages"); locations.foreach(function (location) { num++; console.log("looking location"); passage.find({"placekey": location.properties.placekey}, function (err, passages) { if (err) { console.log('db error finding passage for: ' + location.properties.placekey); } else { console.log("passage found!"); location.properties.passages = passages[0]; //take first passage num--; } if (num === 0) { console.log("all passages found!"); var featurecollection = { "type": "featurecollection", "features": locations }; console.log(featurecollection); console.log(featurecollection.features[0].properties); console.log(featurecollection.features[0].properties.passages); res.json(featurecollection); console.log("json sent over!"); } }); });
logging featurecollection gets me featurecollection without passages:
{ "type": "featurecollection", "features": [ { "type": "feature", "properties": { "num_books": 62, "age": "built 1078", "id": "", "num_mentions": 325, "place": "the tower", "placekey": "thetower", "geocodenotes": "", "notes": "built on site of roman fortifications, central part of tower, known white tower, built in 1078 william conqueror. subsequent rings of fortification added later. used royal residence prison , place of execution until elizabethan times. england's child king, edward v, , brother murdered in tower in 1483 supposedly uncle, richard iii.", "class": "n/a", "type": "landmark" }, "geometry": { "type": "point", "coordinates": [ -0.076111, 51.508056 ] } }, // more objects
no passages property.
however, when use console.log(featurecollection.features[0].properties.passages), first passage:
{ "_id": "51deebdbb2b5de1b8b6d7da1", "index": 27100, "bookid": 1, "author": "ainsworth, william", "place": "the tower", "placekey": "thetower", "query_ok": true, "year": 1839, "corpus": "chadwyck", "fn": "/volumes/liladata1/plain2/chadwyck/lilabookid_00149.txt", "context_a": "the course of carpenter's meditations here... //more features }
moreover, using (if 'passages' in featurecollection.features[0].properties) gives me true. in fact, can condition sending json response server that, , featurecollection without passages sent...
sorry long-winded post, i'm going crazy on this. ideas?
thank you! :)
the problem inspect defined on document interfering console.log operation. inspect not consider properties added document instance (like documentinst.prop=1).
to fix problem try use tojson on return documents , attach properties return object
in case,
var _locations = []; locations.foreach(function(_location){ // actual mongoose document var location; location = _location.tojson(); // tojson on _location mongoose document _locations.push(location); //push object new array _locations ....logic passages... //imp - add properties location object return tojson if (num === 0) { ... var featurecollection = { "type": "featurecollection", "features": _locations // use new _locations array }; ... }
});
Comments
Post a Comment