nodejs + mongoose Tree 递归查询, 无法回调 JSON Tree
发布于 8 小时前 作者 inobox 73 次浏览 来自 问答

刚学了一些 Node 准备将php+mysql 的网站改成 Nodejs + Mongodb 但是基础有些差。。 有些值不能直接调用。。

现在卡在 Category 的 Tree 结构部分。。 我想把 Category 的数据递归查询组合成 JSON 格式后反应到页面上的 。。

回调和递归部分一直不是很明白。。 希望大家能帮我。。

mongoDB 数据

{"_id":"57149a9ef34a638c17097eb4","RB":26,"LB":1,"Level":0,"Title":"asdfasdf","Locale":[],"__v":0}
{"_id":"5714a7f991b2eb602f28f835","RB":21,"LB":6,"Level":1,"Title":"test2","Locale":[],"__v":0}
{"_id":"5714a83b91b2eb602f28f836","RB":5,"LB":2,"Level":1,"Title":"test3","Locale":[],"__v":0}
{"_id":"571856d21ba7b2282a773f1b","RB":23,"LB":22,"Level":1,"Title":"test 4","Locale":[],"__v":0}
{"_id":"571856db1ba7b2282a773f1c","RB":25,"LB":24,"Level":1,"Title":"test 5","Locale":[],"__v":0}
{"_id":"57149a9ef34a638c17097eb4","RB":26,"LB":1,"Level":0,"Title":"asdfasdf","Locale":[],"__v":0}
{"_id":"5714a8d63f064054205b761c","RB":18,"LB":17,"Level":2,"Title":"test 2-1","Locale":[],"__v":0}
{"_id":"5714aa483f064054205b761d","RB":16,"LB":15,"Level":2,"Title":"test 2-2","Locale":[],"__v":0}
{"_id":"5714aa9787dcb2c81b0adae5","RB":14,"LB":13,"Level":2,"Title":"test 2-3","Locale":[],"__v":0}
{"_id":"5714ac5fd820877424fbe8c8","RB":12,"LB":11,"Level":2,"Title":"test 2-4","Locale":[],"__v":0}
{"_id":"5714ac86d820877424fbe8c9","RB":10,"LB":9,"Level":2,"Title":"test 2-5","Locale":[],"__v":0}
{"_id":"5714ac98ddf61df81b674c56","RB":8,"LB":7,"Level":2,"Title":"test 2-6","Locale":[],"__v":0}
{"_id":"5714ad30aa0243b01b5f771b","RB":20,"LB":19,"Level":2,"Title":"test 2-7","Locale":[],"__v":0}
{"_id":"5714a7f991b2eb602f28f835","RB":21,"LB":6,"Level":1,"Title":"test2","Locale":[],"__v":0}
{"_id":"571856541a5c99501b0f6205","RB":4,"LB":3,"Level":2,"Title":"test 3-1","Locale":[],"__v":0}
{"_id":"5714a83b91b2eb602f28f836","RB":5,"LB":2,"Level":1,"Title":"test3","Locale":[],"__v":0}

/*************************************************************************************************************** Build Category Tree Json ****************************************************************************************************************/

var rebuildTree = function(categoryID, Lang, callback){
	var nodeTree = [];		
	var recursiveTree = function(node, Lang){
		var id = node._id, bgi = node.LB, bdi = node.RB, nivi = node.Level;
		Categories.find({ LB: { $gt: bgi }, RB: { $lt: bdi }, Level: nivi + 1 }).lean().exec( function(err, Children){	
			if (err){
				return callback(err);
			}
			if( Children.length === 0 ){
				return;
			}
			Children.forEach( function(item, key){
				return recursiveTree(item, Lang);
			});
			node.child = Children;				
			nodeTree.push(node);
			
			// 这个会回调三次 nodeTree
			return callback(nodeTree);
			
		});
	};
	
	Categories.findOne('{ _id: categoryID }).lean().exec(function(err, res){		
		recursiveTree(res, Lang, function(res){				
			console.log(':::' + JSON.stringify(tree) );
		});
	});
};

/*************************************************************************************************************** 想要得到的效果是 ****************************************************************************************************************/

[{"_id":"57149a9ef34a638c17097eb4","RB":26,"LB":1,"Level":0,"Title":"asdfasdf","Locale":[],"__v":0,
	"child":[
		{"_id":"5714a7f991b2eb602f28f835","RB":21,"LB":6,"Level":1,"Title":"test2","Locale":[],"__v":0,
			"child":[
				{"_id":"5714a8d63f064054205b761c","RB":18,"LB":17,"Level":2,"Title":"test 2-1","Locale":[],"__v":0},
				{"_id":"5714aa483f064054205b761d","RB":16,"LB":15,"Level":2,"Title":"test 2-2","Locale":[],"__v":0},
				{"_id":"5714aa9787dcb2c81b0adae5","RB":14,"LB":13,"Level":2,"Title":"test 2-3","Locale":[],"__v":0},
				{"_id":"5714ac5fd820877424fbe8c8","RB":12,"LB":11,"Level":2,"Title":"test 2-4","Locale":[],"__v":0},
				{"_id":"5714ac86d820877424fbe8c9","RB":10,"LB":9,"Level":2,"Title":"test 2-5","Locale":[],"__v":0},
				{"_id":"5714ac98ddf61df81b674c56","RB":8,"LB":7,"Level":2,"Title":"test 2-6","Locale":[],"__v":0},
				{"_id":"5714ad30aa0243b01b5f771b","RB":20,"LB":19,"Level":2,"Title":"test 2-7","Locale":[],"__v":0}
			]},
		{"_id":"5714a83b91b2eb602f28f836","RB":5,"LB":2,"Level":1,"Title":"test3","Locale":[],"__v":0,
			"child":[
				{"_id":"571856541a5c99501b0f6205","RB":4,"LB":3,"Level":2,"Title":"test 3-1","Locale":[],"__v":0}
			]},
		{"_id":"571856d21ba7b2282a773f1b","RB":23,"LB":22,"Level":1,"Title":"test 4","Locale":[],"__v":0},
		{"_id":"571856db1ba7b2282a773f1c","RB":25,"LB":24,"Level":1,"Title":"test 5","Locale":[],"__v":0}
	]}
}]

/*************************************************************************************************************** 可是现在递归查询时 回调 Callback 三次结果, 还有最终回调的结果 又多出了 test 2 和 test 3 的 Json ****************************************************************************************************************/

[{"_id":"57149a9ef34a638c17097eb4","RB":26,"LB":1,"Level":0,"Title":"asdfasdf","Locale":[],"__v":0,
	"child":[
		{"_id":"5714a7f991b2eb602f28f835","RB":21,"LB":6,"Level":1,"Title":"test2","Locale":[],"__v":0,
			"child":[
				{"_id":"5714a8d63f064054205b761c","RB":18,"LB":17,"Level":2,"Title":"test 2-1","Locale":[],"__v":0},
				{"_id":"5714aa483f064054205b761d","RB":16,"LB":15,"Level":2,"Title":"test 2-2","Locale":[],"__v":0},
				{"_id":"5714aa9787dcb2c81b0adae5","RB":14,"LB":13,"Level":2,"Title":"test 2-3","Locale":[],"__v":0},
				{"_id":"5714ac5fd820877424fbe8c8","RB":12,"LB":11,"Level":2,"Title":"test 2-4","Locale":[],"__v":0},
				{"_id":"5714ac86d820877424fbe8c9","RB":10,"LB":9,"Level":2,"Title":"test 2-5","Locale":[],"__v":0},
				{"_id":"5714ac98ddf61df81b674c56","RB":8,"LB":7,"Level":2,"Title":"test 2-6","Locale":[],"__v":0},
				{"_id":"5714ad30aa0243b01b5f771b","RB":20,"LB":19,"Level":2,"Title":"test 2-7","Locale":[],"__v":0}
			]},
		{"_id":"5714a83b91b2eb602f28f836","RB":5,"LB":2,"Level":1,"Title":"test3","Locale":[],"__v":0,
			"child":[
				{"_id":"571856541a5c99501b0f6205","RB":4,"LB":3,"Level":2,"Title":"test 3-1","Locale":[],"__v":0}
			]},
		{"_id":"571856d21ba7b2282a773f1b","RB":23,"LB":22,"Level":1,"Title":"test 4","Locale":[],"__v":0},
		{"_id":"571856db1ba7b2282a773f1c","RB":25,"LB":24,"Level":1,"Title":"test 5","Locale":[],"__v":0}
]},
//// 多出来的部分 ////
{"_id":"5714a7f991b2eb602f28f835","RB":21,"LB":6,"Level":1,"Title":"test2","Locale":[],"__v":0,
	"child":[
		{"_id":"5714a8d63f064054205b761c","RB":18,"LB":17,"Level":2,"Title":"test 2-1","Locale":[],"__v":0},
		{"_id":"5714aa483f064054205b761d","RB":16,"LB":15,"Level":2,"Title":"test 2-2","Locale":[],"__v":0},
		{"_id":"5714aa9787dcb2c81b0adae5","RB":14,"LB":13,"Level":2,"Title":"test 2-3","Locale":[],"__v":0},
		{"_id":"5714ac5fd820877424fbe8c8","RB":12,"LB":11,"Level":2,"Title":"test 2-4","Locale":[],"__v":0},
		{"_id":"5714ac86d820877424fbe8c9","RB":10,"LB":9,"Level":2,"Title":"test 2-5","Locale":[],"__v":0},
		{"_id":"5714ac98ddf61df81b674c56","RB":8,"LB":7,"Level":2,"Title":"test 2-6","Locale":[],"__v":0},
		{"_id":"5714ad30aa0243b01b5f771b","RB":20,"LB":19,"Level":2,"Title":"test 2-7","Locale":[],"__v":0}
	]
},
{"_id":"5714a83b91b2eb602f28f836","RB":5,"LB":2,"Level":1,"Title":"test3","Locale":[],"__v":0,
	"child":[
		{"_id":"571856541a5c99501b0f6205","RB":4,"LB":3,"Level":2,"Title":"test 3-1","Locale":[],"__v":0}
	]
}]
回到顶部