Javascript对象-整体对象中缺少的功能,但仍然存在
Javascript objects - feature missing from overall object, yet exists
这里有个奇怪的问题。我正在运行Node/Express/Mongoose/Leaflet。我从数据库中提取一个位置数组,一旦回调启动,我就会迭代这些位置,找到一堆处理每个位置的字符串段落。然后,我尝试将段落数组附加到每个位置对象,然后将位置数组附加到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.passions)时,我得到了第一段:
{
"_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")会使我为true。事实上,我可以设置从服务器向其发送JSON响应的条件,并且我的featureCollection(没有段落)将被发送。。。
很抱歉发了这么长的帖子,但我真的快疯了。有什么想法吗?
谢谢!:)
问题是在Document上定义的检查干扰了console.log操作。此检查不考虑添加到文档实例的属性(如documentinst.prp=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
};
...
}
});
当res.json(obj)被调用时,这个方法要做的第一件事就是调用json.stringfy(obj;
如果obj定义了toJSON,那么从调用obj.toJSON()返回的对象将由JSON.stringify.使用
如果您在一个对象上提供一个toJSON方法,并且它被编码为返回另一个对象而不是定义toJSON的对象,JSON.stringify将对这个新对象进行操作。您将能够查看这个新对象的属性,而不是实际对象的属性。
res.json依赖于toJSON。
console.log依赖inspect。
我刚刚用这个代码片段演示了上面的内容,
var util = require('util');
function Document(){this._doc = {};}
Document.prototype.inspect = function(){return util.inspect(this._doc)};
Document.prototype.toJSON = function(){return this.inspect()};
var docInst = new Document();
docInst.newProp = 'abc'; //property never shows up in console.log / JSON.stringify
docInst._doc._newProp = "_abc";
console.log(docInst);
JSON.stringify(docInst);
- 正在加载具有相同功能的多个对象
- .on(“单击”,..)不处理使用相同的单击功能创建的对象
- 为什么这个功能打印“;未定义的未定义的“;作为联系人名称,添加“新对象”
- 如何在对象上实现类似拼接的功能
- 是否有将基于字符串的JSON转换为Mongoose Schema对象实例的本地功能
- 如何使用自定义功能覆盖时间轴对象.js时间轴对象的_repaintMinorText
- 从对象旋转对象.带有自定义动画功能的trix.setrotationfromleuler
- JavaScript清理“”中引用的(本地)对象;延迟的“;功能
- 日志记录功能/对象名称's在函数堆栈内的javascript中
- 使用 JavaScript 中的对象的哈希表功能
- 自动完成功能,用于按属性搜索 json 对象
- 这是一个功能声明、表达式和对象吗?
- Javascript - 更快的功能:线性列表搜索或获取对象值
- 向角度对象添加动态功能
- 是否有类似 .push 的功能可以动态地向对象添加值
- 如何根据茉莉花单元测试中的其他对象测试功能
- 允许向对象添加和删除功能的设计模式
- Javascript点击功能不起作用.在输出中显示错误 [对象对象]
- 如何在GWT(或Java AWT Swing)中使用相同的事件处理程序对相同类型的多个对象执行相同的功能
- 触发通过swfoject'嵌入的flash对象功能;s嵌入SWF