backbone.js和express:遇到一个查询string按字段search一个mongodb集合的麻烦

我是新来的骨干,快递和mongodb。 我想传递一个查询string按字段searchMongoDB集合。 我做错了什么。 如果我从路由器注释掉“获取”,则find页面。 如果我尝试获取,那么我得到一个页面未find错误。 我试图隔离它的中断,但骨干架构仍然让我感到困惑。 提前致谢。 (我打赌这是在我的mongodb调用语法问题)克里斯汀

这是我的代码。

这个URL应该返回“type”= 3的集合。

localhost:8888/#content/3 

型号/ models.js:

 window.Content = Backbone.Model.extend({ urlRoot: "/content", idAttribute: "_id" }); window.ContentCollection = Backbone.Collection.extend({ model: Content, url: "/content" }); 

意见/ content.js

 window.ContentListView = Backbone.View.extend({ initialize: function () { this.render(); }, render: function () { //return this; this.$el.append('<ul class="thumbnails">'); this.collection.each(function(model) { this.$('.thumbnails').append(new ContentView({model: model}).render().el); }, this); return this; } }); window.ContentView = Backbone.View.extend({ tagName: "li", initialize: function () { this.model.bind("change", this.render, this); this.model.bind("destroy", this.close, this); }, render: function () { $(this.el).html(this.template(this.model.toJSON())); return this; } }); 

意见/ main.js

 var AppRouter = Backbone.Router.extend({ routes: { "content/:type" : "contentType" }, contentType: function(type) { var contentList = new ContentCollection({type : type}); contentList.fetch({success: function(){ $("#content").empty().append(new ContentListView({collection: contentList}).el); }}); this.headerView.selectMenuItem('build-menu'); }, utils.loadTemplate([ 'ContentView' ], function() { app = new AppRouter(); Backbone.history.start(); }); 

contentView.html

 name (<% tag won't print here) 

路线/ modules.js

 exports.findContentByType = function(req, res) { var type = req.params.type; db.collection('content', function(err, collection) { collection.find({'type': type.toString()}).toArray(function(err, items) { res.send(items); }); }); }; 

server.js

 app.get('/content/:type', module.findContentByType); 

我可以在这里看到几个问题:

  1. this.headerView.selectMenuItem('build-menu'); (在路由器中)意味着你已经在路由器对象中定义了headerView ,但是没有定义它。
  2. 同样, ContentView this.template没有定义

当我删除#1中的行,并在ContentView定义一个虚拟模板时:

 template: _.template("<div> Test: <%= version %> </div>"), 

然后,这个视图至less呈现 – 看到这里 。 (这是与虚拟数据 – 我无法确认您的服务器正在返回有效/预期的JSON。)