[英]placing 1 mongodb query inside express.Router().get() results in 2 requests
When I place a MongoDB query inside router.get() in /routes/index.js
like this: 当我把里面router.get()一个MongoDB的查询
/routes/index.js
是这样的:
/* GET home page. */
router.get('/', function(req, res, next) {
console.log('1 rendering index performing a DB query');
performQuery(query, function(dbData) {
res.render('./home/index', dbData);
});
});
I get this console output: 我得到以下控制台输出:
[nodemon] starting `node ./bin/www`
app.use(/, routes)
SERVER READY on port 3000
SOCKET.IO client: /#V261JQOrV3aEyu34AAAA CONN
rendering index performing a DB query -------------------------------------------------> 1
GET / 200 744.652 ms - 4054 -----------------------------------------------------------> 1
SOCKET.IO client: /#V261JQOrV3aEyu34AAAA DISCONN
GET /stylesheets/main.css 304 4.166 ms - -
GET /images/logo-icon-90.png 304 0.514 ms - -
GET /public/js/libs/socket.io/socket.io.js 304 3.888 ms - -
GET /public/js/libs/jquery/jquery-2.1.4.min.js 304 15.758 ms - -
rendering index performing a DB query -------------------------------------------------> 2
GET / 304 363.686 ms - - --------------------------------------------------------------> 2
SOCKET.IO client: /#w4kVulgNfEfnJJPOAAAB CONN
I checked if there was another error of some sort taking out the MongoDB query and just rendering '/' and then console outputs just one GET request to '/'. 我检查是否还有其他错误,将MongoDB查询取出并仅渲染“ /”,然后控制台仅向“ /”输出一个GET请求。 The only thing changing between getting 1 or 2 GET request is placing the MongoDB query.
在获取1个或2个GET请求之间唯一改变的是放置MongoDB查询。
performQuery.js
content: performQuery.js
内容:
MongoClient.connect(mongoURL + dbName, function(err, db) {
assert.equal(err, null);
const cursor = db.collection(collection).aggregate([
{ $match: query.justVars},
{ $group: {
_id: {var1: '$somevar', var2: '$anothervar'},
takeScore: {$first: '$score'}
}
},
{ $sort: {score: -1}},
{ $group: {
_id: '$_id.var1',
vars2: { $push: {
name: '$_id.var2',
score: '$takeScore'
}
},
sumScore: { $sum: '$score'}
}
},
{ $sort: {sumScore: -1}}
]);
cursor.toArray(function(error, documents) {
assert.equal(error, null);
callback(documents);
db.close();
});
});
QUESTION 题
Why is there 2 GET request to '/' and not just one? 为什么对“ /”有2个GET请求,而不仅仅是一个?
A possible problem is the cursor.toArray
. 一个可能的问题是
cursor.toArray
。 Can you console.log
here: 您可以在此处
console.log
吗:
cursor.toArray(function(error, documents) {
assert.equal(error, null);
console.log(documents);
callback(documents);
db.close();
});
and check how many extra lines do you have in the console? 并检查控制台中有多少行?
It seems that you retrieve 2 documents from the database and in this iteration you call 2 times the callback. 似乎您从数据库中检索了2个文档,并且在此迭代中,您调用了2次回调。 You have to call the callback ONLY when the iteration will finish.
您仅在迭代完成时才调用回调。
What is the SOCKET.IO logs? 什么是SOCKET.IO日志? Is it possible that you have open 2 different tabs on the browser and when you restart the application they are trying to connect automatically and that's why it feels like you have 2 GET requests?
您是否有可能在浏览器上打开2个不同的选项卡,并且在重新启动应用程序时它们试图自动连接,这就是为什么感觉您有2个GET请求?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.