0

フロントエンドでバックボーンを使用しているMongoDBでNodeJS用のREST APIを作成しています。デモモデルです

var MenuItem = Backbone.Model.extend({

    idAttribute: "_id",

    urlRoot: '/sentences'

});

これは、そのモデルで fetch を呼び出すビュー コードです (このコードでは、そのモデルの作成を示していないことに注意してください)。mongo ドキュメントの 1 つに _id をハードコードしました

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

menuItem.fetch()が呼び出されたときに投稿用に生成される URL は次のとおりです。

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

以下は の json データでlocalhost:8080/sentencesあるため、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 で検索するように変更したため、この URL (mongo ID を使用) はまだ機能していません。"http://localhost:8080/sentences/526c0e21977a67d6966dc763"

app.get('/sentences/:id', function(req,res){


     var query = { '_id' : req.params.id };


    db.collection('english').findOne(query, function(err, doc) {
        if(err) throw err;

        console.dir(doc);
        res.send(doc);


    });


});
4

0 に答える 0