这里奇怪的问题。我正在运行Node / Express / Mongoose / Leaflet。我从数据库中提取了一系列位置,一旦启动了回调,便会遍历这些位置,以找到一串处理每个位置的字符串段落。然后,我尝试将段落数组附加到每个location对象,然后将locations数组附加到GeoJSON FeatureCollection。

Location.find({}, { _id: 0 }, function (err, locations) {
    if (err) {
        console.log('DB Error loading all locations');
    res.redirect('/');
} else {
    var num = 0;
    console.log("Beginning finding all passages");
    locations.forEach(function (location) {
    num++;
    console.log("Looking up a 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 was found!");
            location.properties.passages = passages[0]; //take first passage
            num--;
        }
        if (num === 0) {
            console.log("All passages were 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!");
        }
    });
});


记录featureCollection可让我获得我的featureCollection,而无需任何段落:

{
  "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 the site of Roman fortifications, the central part of the Tower, known as the White Tower, was built in 1078 by William the Conqueror. Subsequent rings of fortification were added later. It was used as a royal residence as well as a prison and place of execution until Elizabethan times. England's child king, Edward V, and his brother were murdered in the Tower in 1483 supposedly by their uncle, Richard III.",
        "Class": "n/a",
        "Type": "Landmark"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -0.076111,
          51.508056
        ]
      }
    },
   // more objects


没有段落属性。

但是,当我使用console.log(featureCollection.features [0] .properties.passages)时,我得到了第一篇文章:

 {
  "_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 the carpenter's meditations was here...
  //more features
}


此外,使用(如果在featureCollection.features [0] .properties中为“ passages”)可以使我成真。实际上,我可以限制从服务器向其发送JSON响应,并且不带段落的我的featureCollection将被发送...

很抱歉这篇冗长的文章,但是我真的为此感到疯狂。有任何想法吗?

谢谢! :)

最佳答案

问题是,在Document上定义的检查会干扰console.log操作。该检查不考虑添加到文档实例的属性(例如documentinst.prop = 1)。

要解决您的问题,请尝试在返回文档上使用toJSON,然后将属性附加到返回对象

就你而言

var _locations = [];

locations.forEach(function(_location){ // actual mongoose document

  var location;

  location = _location.toJSON(); // do toJSON on the _location mongoose document

  _locations.push(location); //push this object into the new array _locations

  ....logic for passages...

  //imp - add properties to the location object return from toJSON

  if (num === 0) {
    ...
     var featureCollection = {
            "type": "FeatureCollection",
            "features": _locations // use the new _locations array
      };

    ...
 }


});

07-26 04:52