我有一个递归的客户端功能,可以跨集合跟踪相关文档的网络。我遇到了两个问题,这是一个问题:
var fam=Families.find(qry).fetch();
console.log(fam);
qrt的形式为:
{gedcomId: "F27"}
第一次运行查找时,我得到:
Array [ Object ]
其中Object是应该找到的一个有效文档。 这是一个更完整的代码示例:
Template.viewer.Nodes = [];
Template.viewer.Links = [];
Template.viewer.labelAnchors = [];
Template.viewer.labelAnchorLinks = [];
var NodesReverse = {};
var LinksReverse = {};
var processPersonNode = function(qry) {
console.log('processPersonNode');
console.log(qry.gedcomId);
var node_id = qry.gedcomId;
var weight = 0;
if (!NodesReverse.hasOwnProperty(node_id)) {
weight = 1;
var person = Persons.find(qry, {
sort: {
nameLastBirth: 1,
nameFirstBirth: 1
},
fields: {
_id: true,
nameLastBirth: true,
nameFullGedcom: true,
nameFirstBirth: true,
FAMC: true,
FAMS: true,
gedcomId: true,
sex: true,
Birth: true,
Death: true
}
}).fetch();
console.log(person[0]);
var this_node = _.clone(person[0]);
this_node.node_type = 'person';
NodesReverse[person[0].gedcomId] = {};
NodesReverse[person[0].gedcomId].pos = Template.viewer.Nodes.push(this_node);
console.log(NodesReverse);
if (!empty(person[0].FAMC)) {
console.log('processPersonNode:parental fam found');
processFamilyNode({
gedcomId: person[0].FAMC
});
}
if (!empty(person[0].FAMS)) {
var famlen = person[0].FAMS.length;
for (var j = 0; j < famlen; j++) {
console.log('processPersonNode:spousal fam found');
weight += processFamilyNode({
gedcomId: person[0].FAMS[j].entrydata
});
}
}
NodesReverse[person[0].gedcomId].weight = weight;
console.log('processPersonNode - exit');
return weight;
} else {
console.log('processPersonNode already exists- exit');
return NodesReverse[qry.gedcomId].weight;
}
};
var processFamilyNode = function(qry) {
var weight = 0;
console.log('processFamilyNode');
console.log(qry);
console.log();
console.log(NodesReverse);
var node_id = qry.gedcomId + '';
console.log(NodesReverse[node_id]);
if (!NodesReverse.hasOwnProperty(node_id)) {
var fam = Families.find(qry).fetch();
console.log(fam);
// add node
var this_node = _.clone(fam[0]);
this_node.node_type = 'family';
NodesReverse[fam[0].gedcomId] = {};
NodesReverse[fam[0].gedcomId].pos = Template.viewer.Nodes.push(this_node);
console.log(NodesReverse);
// create links
if (!empty(fam[0].Husband)) {
console.log('processFamilyNode:husband found');
createLink(fam[0].gedcomId, fam[0].Husband, 'spouse', 4);
weight += processPersonNode({
gedcomId: fam[0].Husband
});
};
if (!empty(fam[0].Wife)) {
console.log('processFamilyNode:wife found');
createLink(fam[0].gedcomId, fam[0].Wife, 'spouse', 4);
weight += processPersonNode({
gedcomId: fam[0].Wife
});
};
//recurse
if (!empty(fam[0].Children)) {
var childlen = fam[0].Children.length;
for (var j = 0; j < childlen; j++) {
console.log('processFamilyNode:child found');
var ltype = 'child';
if (!empty(fam[0].Children[j].isAdopted))
ltype = 'child-adopted';
createLink(fam[0].gedcomId, fam[0].Children[j].entrydata, ltype, 2);
weight += processPersonNode({
gedcomId: fam[0].Children[j]
});
}
}
NodesReverse[fam[0].gedcomId].weight = weight;
console.log('processFamilyNode - exit');
return weight;
} else {
console.log('processFamilyNode already exists- exit');
return NodesReverse[qry.gedcomId].weight;
}
};