【发布时间】:2016-05-19 14:57:47
【问题描述】:
我正在使用 nodejs、mongodb 和 ejs 视图引擎创建一个求职门户: 集合“过滤器”=“用户名”“标签” 集合“所有工作”=“类别”“工作数据”
在下面的代码中,我从"filters" 集合中获取'tags',并与"alljobs" 集合中的所有'category' 进行比较。然后所有与'category' 匹配的'tags' 数组使用ejs 视图引擎查看各自的'jobsdata'。
问题: 代码正在运行,但在浏览器中我看不到所有匹配的类别相应的工作,只查看了一个类别的工作数据。我无法理解问题出在哪里?
代码:
function processRequest(req,res){
var tags,data,jobsdata = [];
var len;
if(condition)
{....}
else{
var db = new Db('askhere', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/askhere';
client.connect(url, function (err, db) {
var col = db.collection('filters');
col.find({username:req.session.userName}).toArray(function (err, items) { // find tags of session user from collection filters
console.log('items: ' + JSON.stringify(items));
items.forEach(function (doc) {
tags = doc.tags; //save tags
});
var col = db.collection('alljobs'); //find all categories jobs matched with tags data in collection alljobs
for(var i=0; i<tags.length;i++){
col.find({category:tags[i]}).toArray(function (err, items1) {
if (items1 == false) {
res.render('mainqa',{uname:req.session.userName,tags:'No Tags Selected',jobsdata:'No Tags Matched !!!',len:0});
}
items1.forEach(function (doc1) {
jobsdata = doc1.jobsdata;
var html = ["url : ", "Posted Date : ", "Job Title : ", "Company : ", "Location : "]
for (var i = 0; i < 25; i++) {
for (var j = 0; j < 5; j++) {
data.push(html[j] + jobsdata[i][j]);
} //Nested for loop
} //for loop covert 2D array in 1D
res.render('mainqa',{uname:req.session.userName,tags:tags,jobsdata:data,len:len});
}); //forEach
}); //col.find collection=alljobs
} //for loop
}); //col.find collection=filters
}); //client connect
}); //db.authenticate
}); //db.open
} //else end
} //function processRequest end
【问题讨论】:
标签: javascript node.js mongodb ejs