带有json数据数组的骨干Ajax请求

时间:2013-10-28 18:09:29

标签: node.js mongodb backbone.js

我正在使用前端使用Backbone的MongoDB为NodeJS制作REST api。这是演示模型

var MenuItem = Backbone.Model.extend({

    idAttribute: "_id",

    urlRoot: '/sentences'

});

这是在该模型上调用fetch的视图代码(注意我没有在此代码中显示该模型的创建)。我已经为其中一个mongo文件硬编了_id

itemDetails: function (event){
    this.menuItemModel.set('_id', '526c0e21977a67d6966dc763');
    this.menuItemModel.fetch();

当帖子menuItem.fetch()被调用时,为帖子生成的网址是

XHR finished loading: "http://localhost:8080/sentences/526c0e21977a67d6966dc763". 

下面是localhost:8080/sentences的json数据,因此对具有Mongo对象id的url的xhr请求不返回任何内容。但是,如果我执行localhost:8080/sentences/1,它将返回json数据数组中的第一个。

[
  {
    "_id": "526c0e21977a67d6966dc763",
    "question": "1",
    "uk": "I heard a bloke on the train say that tomorrow's trains will be delayed.",
    "us": "I heard a guy on the train say that tomorrow's trains will be delayed."
  },
  {
    "_id": "526c0e21977a67d6966dc764",
    "question": "2",
    "uk": "Tom went outside for a fag. I think he smokes too much!",
    "us": "Tom went outside for a cigarette. I think he smokes too much!"
  },
  {
    "_id": "526c0e21977a67d6966dc765",
    "question": "3",
    "uk": "Do you fancy going to the cinema on Friday?",
    "us": "How about going to the movies on Friday"
  }
]

问题:当我在模型上调用fetch时,为什么Backbone不会自动返回记录?

更新

这是server.js中返回句子

的node.js方法/路由
app.get('/sentences', function (req, res){

     db.collection('english', function(err, collection) {
        collection.find().toArray(function(err, items) {

            res.send(items);

        });
    });

})

更新

这是搜索单个记录的功能。它曾用question进行搜索(此时/sentences/1返回了一条记录),但现在我已将其更改为_id搜索,此网址(带有mongo ID)仍无法正常工作{{ 1}}

"http://localhost:8080/sentences/526c0e21977a67d6966dc763"

0 个答案:

没有答案