【问题标题】:Backbone.js with MongoDB passing req.params into exports functions带有 MongoDB 的 Backbone.js 将 req.params 传递给导出函数
【发布时间】:2013-10-23 01:20:34
【问题描述】:

我正在尝试向 express.js、backbone.js 应用程序中的 mongodb 查找的“导出”方法发送请求参数。我有一个困难 是时候将参数传递给 mongodb 并使用“#”。

破坏是将参数传递到导出的mongodb函数中。

这是数据流:

首先请求被成功路由到'upcoming'函数:

    "upcoming/uni/:uni" : "upcoming",

它毫无问题地流向“即将到来的”功能。

    upcoming: function(uni) {
    console.log("uni: "+uni);
    pag.reset();
    console.log("Hit upcoming list target");
    setCollectionType('upcoming');
        var upcomingCourses = buildCollection();

    // ------------------------------------------------------------------------
    // here is the problem how do I pass the parameter value through the fetch?
    // Although it may also have to do with '#' please read on.
    // ------------------------------------------------------------------------
        upcomingCourses.fetch({success: function(){
            $("#content").html(new ListView({model: upcomingCourses, page: 1}).el);
         }});
    this.headerView.selectMenuItem('home-menu');
},

mongo 方法的路由是:

app.get('/upcoming/uni/:uni', mongomod.findUpcoming);

所以下面的方法是从mongodb js文件中导出的,执行可靠。但是 req.params 没有通过。 穿插在代码中我已经描述了它的运行时行为:

exports.findUpcoming = function(req, res) {
    console.log("university", req.params.uni); // This consistently is unpopulated
    var uni = req.params.uni;
    console.log("Size: "+req.params.length); // This will always be 0
    for (var i=0; i < req.params.length; i++) {
        console.log("Parameters: "+req.params[i]);
    }

    db.collection('upcoming', function(err, collection) {

    if (typeof uni === 'undefined') {
        console.log("The value is undefined");
        uni = "Princeton University"; // here we add a string to test it it will work.
    }

    collection.find({university:uni}).toArray(function(err, items) {
        if (err) {
            console.log("Error: "+err);
        } else {
            console.log("No Error");
            console.log("Count: "+items.length);
            console.log(items[0]['university']);
            res.send(items);
        }
     });
  });
};

关于附加和重要说明:

在运行时环境中的 url 应该是:

http://localhost:3000/#upcoming/uni/Exploratorium

这个失败了,但是下面的 URL 可以通过这些函数传递参数,但是它将 JSON 返回到屏幕而不是 渲染版本:

http://localhost:3000/upcoming/uni/Exploratorium

问题可能是对 # 和模板的错误理解。请,如果您看到错误启发将不胜感激。

【问题讨论】:

标签: mongodb backbone.js express parameter-passing params


【解决方案1】:

# 传递给服务器后什么都没有。请参阅How to get hash in a server side language?https://stackoverflow.com/a/318581/711902

【讨论】:

  • 问题是 URL 参数没有在内部传递。您的回答确实阐明了为什么上述 URL 之一成功,没有哈希,因为它直接进入服务器(谢谢),并在问题上提出了更好的观点。
【解决方案2】:

我找到了解决从客户端传递参数到服务器端的问题。通过更改集合的 url,参数将被传递到服务器端:

upcomingCourses.url = "/upcoming/uni/"+uni; // <-- here's the ticket where uni is param
    upcomingCourses.fetch({success: function(){
        $("#content").html(new ListView({model: upcomingCourses, page: 1}).el);
 }});

这可以做得更优雅,但它是一种将参数传递给服务器的方法。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 2016-06-29
    • 2016-08-07
    • 2020-11-08
    • 2014-10-24
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多