Backbone.js collection not fetching successful by id

217 Views Asked by At

So i have mongodb collection of attendees

_id
event_id
name
profile_picture
user_id

I am routing two GET attendees collection

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\attendee\528b4f85dafbffbc12000009 gives me something like this

[
  {
    "_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"
  }
]

I created a Backbone Model class for attendee with collection

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
        };    
});

Then a ListView Class to List all the Attendees. So there are two View here one is the list view and the other is the List View Item.

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
    };

});

I am rending this function in other file where EventAttendees points to the above view file

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); 
    }
});

The Problem is the collection is not fetch successful and hence below lines are not executed and i am getting an error --http://pastebin.com/HDaQaWcW(can someone also help how to find the make some sense of the error?)

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

EDIT: as per @homtg i got a new update.

I was getting an error because there was no json Its seems that my backbonejs is not doing localhost:3000\attendee\528b4f85dafbffbc12000009 insert doing a localhost:3000\attendee. How do i tell the collection to fetch by event_id?

1

There are 1 best solutions below

0
On
idAttributemodel.idAttribute 

A model's unique identifier is stored under the id attribute. If you're directly communicating with a backend (CouchDB, MongoDB) that uses a different unique key, you may set a Model's idAttribute to transparently map from that key to id.

var Meal = Backbone.Model.extend({
  idAttribute: "_id"
});

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