我正在使用NodeJS和MongoDB。我通过Mongoose连接到MongoDb。
我目前想要做的是显示过滤结果。
我实际遇到的问题是/ main页面似乎在过滤后被调用,因为我显示了所有结果并且结果与我的控制台输出不匹配(因此不匹配过滤结果)。 我不知道为什么。通常,renderResults应该只在过滤后呈现主页面。
var auth = function(req, res, next) {
if (req.session && req.session.user) {
return next();
} else {
res.status(401);
return res.render("pages/noaccess");
}
};
问题所依据的基本情境是:
app.get('/main', auth, function(req, res) {
getItems(req, res, function(results) {
renderResults(req, res, results);
});
});
function renderResults(req, res, results) {
res.render('pages/main', {
offerings: results.items,
ownUser: {
id: req.session.user || ""
},
filterObj: {
isActivated: typeof results.filterObj !== 'undefined',
filterFor: typeof results.filterObj !== 'undefined' ? results.filterObj.filter : "",
filterText: typeof results.filterObj !== 'undefined' ? results.filterObj.filterText : ""
}
});
}
我为提供过滤所写的内容是:
app.post('/filterItems', auth, function(req, res) {
console.log("Filterrequest received.");
getItems(req, res, function(results) {
renderResults(req, res, results);
});
});
function getItems(req, res, callback) {
connectToDB(function() {
retrieveItems(req, res, function(result) {
console.log(result.items);
console.log("offerings retrieved");
if (callback && isFunction(callback)) {
callback(result);
}
});
});
}
var connectToDB = function(callback) {
var url = 'mongodb://localhost:27017/Test';
if (!dbconnected) {
console.log("No Databaseconnection existing. Going to create one...");
MongoClient.connect(url, {
useMongoClient: true
});
database = MongoClient.connection;
database.on('connected', function() {
dbconnected = true;
console.log("Successfully Connected to database.");
if (callback && isFunction(callback)) {
callback(database);
}
});
} else {
if (callback && isFunction(callback)) {
callback(database);
}
}
},
retrieveItems = function(req, res, callback) {
var baseCond = req && req.query,
tOfferType = (baseCond && req.query.type && ("wanted" === req.query.type.toLowerCase())) ? "wanted" : "offerings",
filter = req.body && req.body['filter'] && req.body['filterText'] ? {
filter: req.body['filter'],
filterText: req.body['filterText']
} : undefined,
isFullTextSearch = filter && ("nofilter" === filter.filter || "All" === filter.filter || !filter.filter.length);
console.log("offertype variable:" + tOfferType);
if(filter) {
console.log("!!! Filtervalue: " + filter.filter);
} else {
console.log("!!! No filtervalue received!");
}
if (filter && filter.filter && filter.filter.length && filter.filterText && filter.filterText.length) {
console.log("Using fulltextsearch now..");
console.log("Fulltextsearch activated? " + isFullTextSearch);
doFilterRetrieve(tOfferType, filter, isFullTextSearch, function(retrievedFilteredResults) {
if (callback && isFunction(callback)) {
callback({
items: retrievedFilteredResults,
offertype: tOfferType,
filterObj: filter
});
}
});
} else {
console.log("Using normal retrieve now..");
database.collection(tOfferType).find({}).toArray(function(err, results) {
if (err) throw err;
if (callback && isFunction(callback)) {
callback({
items: results,
offertype: tOfferType,
filterObj: filter
});
}
});
}
}
登录后(看起来很不错):
User is logged in: Yes
redirect to /main
No Databaseconnection existing. Going to create one...
Successfully Connected to database.
offertype variable:offerings
!!! No filtervalue received!
Using normal retrieve now..
offerings retrieved
[ { _id: 5a0469ca487f333bb4c16e6b,
start:
{ city: 'Aachen',
zipcode: '37464',
datetime: '2017-11-11T07:00',
street: 'Salaweg 2' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Jenny Baldau',
email: 'skrur@web.de',
mobilenumber: '0183474875',
description: 'gtgt',
costs: '2,00',
freeplacesamount: '3',
personID: 'fiz_baldau42',
created: 2017-11-09T14:44:26.915Z } },
{ _id: 5a046ab057d3a61d8c23eafb,
start:
{ city: 'München',
zipcode: '02837',
datetime: '2017-11-11T07:00',
street: 'Menchweg 23' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Daniel Ebert',
email: 'daniel.ebert@web.de',
mobilenumber: '017354536374',
description: 'Ich biete eine Mitfahrgelegenheit aus München',
costs: '2,00',
freeplacesamount: '3',
personID: 'fia51ebert',
created: 2017-11-09T14:44:26.915Z } } ]
提交全文搜索后。 Searchterm是" Jenny":
Filterrequest received.
offertype variable:offerings
!!! Filtervalue: All
Using fulltextsearch now..
Fulltextsearch activated? true
doFilterRetrieve mit Fulltextsuche...
fulltextsuche nach :Jenny
Zeige ergebnisse aus Volltextsuche:
[ { _id: 5a0469ca487f333bb4c16e6b,
start:
{ city: 'Aachen',
zipcode: '37464',
datetime: '2017-11-11T07:00',
street: 'Salaweg 2' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Jenny Baldau',
email: 'skrur@web.de',
mobilenumber: '0183474875',
description: 'gtgt',
costs: '2,00',
freeplacesamount: '3',
personID: 'fiz_baldau42',
created: 2017-11-09T14:44:26.915Z } } ]
offerings retrieved
[ { _id: 5a0469ca487f333bb4c16e6b,
start:
{ city: 'Aachen',
zipcode: '37464',
datetime: '2017-11-11T07:00',
street: 'Salaweg 2' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Jenny Baldau',
email: 'skrur@web.de',
mobilenumber: '0183474875',
description: 'gtgt',
costs: '2,00',
freeplacesamount: '3',
personID: 'fiz_baldau42',
created: 2017-11-09T14:44:26.915Z } } ]
offertype variable:offerings
!!! No filtervalue received!
Using normal retrieve now..
offerings retrieved
[ { _id: 5a0469ca487f333bb4c16e6b,
start:
{ city: 'Aachen',
zipcode: '37464',
datetime: '2017-11-11T07:00',
street: 'Salaweg 2' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Jenny Baldau',
email: 'skrur@web.de',
mobilenumber: '0183474875',
description: 'gtgt',
costs: '2,00',
freeplacesamount: '3',
personID: 'fiz_baldau42',
created: 2017-11-09T14:44:26.915Z } },
{ _id: 5a046ab057d3a61d8c23eafb,
start:
{ city: 'München',
zipcode: '02837',
datetime: '2017-11-11T07:00',
street: 'Menchweg 23' },
destination:
{ city: 'Köln',
zipcode: '51105',
datetime: '2017-11-12T07:00',
street: 'Westerwaldstraße 92' },
baseData:
{ isOffer: true,
name: 'Daniel Ebert',
email: 'daniel.ebert@web.de',
mobilenumber: '017354536374',
description: 'Ich biete eine Mitfahrgelegenheit aus München',
costs: '2,00',
freeplacesamount: '3',
personID: 'fia51ebert',
created: 2017-11-09T14:44:26.915Z } } ]
你能帮助我吗?