页面更改后获取 MongoDB 个集合
Getting MongoDB collection after change in page
现在我正在使用 Jade、NodeJS 和 MongoDB 与 Monk 一起创建一个网站。
我想要发生的是页面中有一个更新(在这种情况下,用户移动了 google 地图),然后使用新参数(更新纬度和经度),然后该集合用于更新页面上的项目(搜索显示新纬度经度区域中的项目)
这是 get 的 nodejs 代码,目前只显示数据库中的所有内容
router.get('/search', function(req, res) {
var db = req.db;
var collection = db.get('items')
collection.find({},{},function(e,docs){
res.render('searchAll', {
"itemlist" : docs
});
});
});
和玉码
each item, i in itemlist
#{item.stuff}
和一些 javascript 代码,当用户移动地图(并获取新的纬度和经度)时激活,我不会显示,因为它有效。
那么我如何用新的经纬度调用数据库并重新 运行 玉代码,以显示更新的 itemList?
编辑
我现在可以从数据库中获取数据,并将其显示在页面上,但是如何让jade(或js?)对新数据进行迭代并显示在页面上?
button(onclick="httpGet('/afibo5')")
| Click
p#response
script(type='text/javascript').
function httpGet(theUrl){
//document.write(theUrl);
$.get(
theUrl,
{ id: "532169"},
function(responseText){
document.getElementById("response").innerHTML=responseText;
}
);
}
试试这个:
var _ = require("lodash");
router.get('/search', function(req, res) {
req.query = _.pick(req.query, ['lat', 'lon']); // filter query param for exact parameters, if they will not exist it will be empty object
// query mongo for documents with given lat, lon
req.db.get('items').find(req.query,{},function(e,docs){
res.render('searchAll', { "itemlist" : docs });
});
});
// for ajax calls, responds with json, that have to be iterated and rendered using frontend code
router.get('/api/search', function(req, res) {
req.query = _.pick(req.query, ['lat', 'lon']);
req.db.get('items').find(req.query,{},function(e,docs){
res.json({ "itemlist" : docs });
});
});
绑定点击事件做:window.location.href='/search?lat=5&lon=10';
现在我正在使用 Jade、NodeJS 和 MongoDB 与 Monk 一起创建一个网站。
我想要发生的是页面中有一个更新(在这种情况下,用户移动了 google 地图),然后使用新参数(更新纬度和经度),然后该集合用于更新页面上的项目(搜索显示新纬度经度区域中的项目)
这是 get 的 nodejs 代码,目前只显示数据库中的所有内容
router.get('/search', function(req, res) {
var db = req.db;
var collection = db.get('items')
collection.find({},{},function(e,docs){
res.render('searchAll', {
"itemlist" : docs
});
});
});
和玉码
each item, i in itemlist
#{item.stuff}
和一些 javascript 代码,当用户移动地图(并获取新的纬度和经度)时激活,我不会显示,因为它有效。
那么我如何用新的经纬度调用数据库并重新 运行 玉代码,以显示更新的 itemList?
编辑
我现在可以从数据库中获取数据,并将其显示在页面上,但是如何让jade(或js?)对新数据进行迭代并显示在页面上?
button(onclick="httpGet('/afibo5')")
| Click
p#response
script(type='text/javascript').
function httpGet(theUrl){
//document.write(theUrl);
$.get(
theUrl,
{ id: "532169"},
function(responseText){
document.getElementById("response").innerHTML=responseText;
}
);
}
试试这个:
var _ = require("lodash");
router.get('/search', function(req, res) {
req.query = _.pick(req.query, ['lat', 'lon']); // filter query param for exact parameters, if they will not exist it will be empty object
// query mongo for documents with given lat, lon
req.db.get('items').find(req.query,{},function(e,docs){
res.render('searchAll', { "itemlist" : docs });
});
});
// for ajax calls, responds with json, that have to be iterated and rendered using frontend code
router.get('/api/search', function(req, res) {
req.query = _.pick(req.query, ['lat', 'lon']);
req.db.get('items').find(req.query,{},function(e,docs){
res.json({ "itemlist" : docs });
});
});
绑定点击事件做:window.location.href='/search?lat=5&lon=10';