Backbone.js集合未按id成功获取

Backbone.js collection not fetching successful by id

本文关键字:id 成功 获取 js 集合 Backbone      更新时间:2023-11-17

所以我有与会者的mongodb集合

_id
event_id
name
profile_picture
user_id

我正在发送两个GET与会者集合

app.get('/attendee', event.eventAttendee);
app.get('/attendee/:event_id', event.findAttendeeByEventId);
...
...
exports.findAttendeeByEventId = function(req, res) {
    var id = req.params.event_id;
    console.log('Retrieving attendee: ' + id);
    db.collection('attendees', function(err, collection) {
        collection.find({'event_id': new BSON.ObjectID(id)}, function(err, item) {
            item.toArray(function(err, items){
                res.send(items);
            });
        });
    });
};
exports.eventAttendee = function(req, res) {
    res.send(200);
};

localhost:3000''aattee''528b4ff85dafbfc12000009给了我这样的

[
  {
    "_id": "528b8c15e639d70e484b65ac",
    "event_id": "528b4f85dafbffbc12000009",
    "name": "John Doe",
    "profile_picture": "https://graph.facebook.com/1205633679/picture",
    "user_id": "5289e8bbdc91fe1803000009"
  },
  {
    "_id": "528b8c58e639d70e484b65ad",
    "event_id": "528b4f85dafbffbc12000009",
    "name": "Mary Doe",
    "picture": "https://graph.facebook.com/100000484671087/picture",
    "user_id": "528a0b4e4833678c11000009"
  }
]

我用集合为与会者创建了一个骨干模型类

define([
    'backbone',
    'bootstrap',
    'app/helpers/config'
], function(Backbone, config) {
        var Attendee = Backbone.Model.extend({
            urlRoot: "/attendee",
            idAttribute: "event_id"
        });
        var AttendeeCollection = Backbone.Collection.extend({
            model: Attendee,
            url: "/attendee"
        });
        return {
                model: Attendee,
                collection: AttendeeCollection
        };    
});

然后创建ListView类以列出所有与会者。所以这里有两个视图,一个是列表视图,另一个是"列表视图项"。

define([
    'jquery',
    'underscore',
    'backbone',
    'backbone.listview',
], function($,
            _, 
            Backbone,
            ListView) 
    {
    var ListView = Backbone.ListView.extend({});
    var EventAttendeeListItemView = Backbone.View.extend({
        tagName: "div",
        className: "dish col-md-4",
        initialize: function () {
            this.model.bind("change", this.render, this);
            this.model.bind("destroy", this.close, this);
        },
        render: function () {
            this.$el.html('Name: ' + this.model.get('name'));
            return this;
        }
    });
    return{
        ListView: ListView,
        ListItemView: EventAttendeeListItemView
    };
});

我正在EventAttendees指向上述视图文件的其他文件中呈现此函数

var attendee = new Attendee.collection({
    event_id: id
});
var listView = new EventAttendees.ListView({
  className: 'list',
  collection: attendee,
  itemView: EventAttendees.ListItemView
});
attendee.fetch({
    success: function(){
        console.log("listView.render().el");   
        console.log(listView.render().el);
    },
    error: function(){
       console.log("arguments"); 
       console.log(arguments); 
    }
});

问题是收集没有成功获取,因此下面的行没有执行,我得到了一个错误--http://pastebin.com/HDaQaWcW(有人能帮助如何找到错误吗?)

    console.log("listView.render().el");   
    console.log(listView.render().el);

编辑:根据@homtg,我得到了一个新的更新。

我收到一个错误,因为没有json我的backbonejs似乎没有在做localhost:3000''attedee''528b4f85dafbc12000009插入操作localhost:3000''Attedee。如何告诉集合按event_id获取

idAttributemodel.idAttribute 

模型的唯一标识符存储在id属性下。如果您直接与使用不同唯一密钥的后端(CouchDB、MongoDB)通信,则可以将Model的idAttribute设置为从该密钥透明地映射到id。

var Meal = Backbone.Model.extend({
  idAttribute: "_id"
});
var cake = new Meal({ _id: 1, name: "Cake" });
alert("Cake id: " + cake.id);