0

I am developing a Node.js/Backbone.js SPA and moving some of my data from JSON files to Mongo. I have set up an api with Mongoose and my data is posting to the server. However, when I try to fetch the data from Mongo it only returns the last item in my collection.

The only change in my Backbone.js code is the url I define in the Backbone Collection.

Old Collection that fetched all JSON data without a problem:

var SupplyCategoriesCollection = Backbone.Collection.extend({

    model: SupplyCategory,
    url: '/supplies',

}); 

New Collection only fetches last item in Mongo database

var SupplyCategoriesCollection = Backbone.Collection.extend({

    model: SupplyCategory,
    url: '/api/products',

}); 

When I navigate to http://localhost:3000/api/products I get the exact same data as when I navigate to http://localhost:3000/supplies.

Stennie
  • 63,885
  • 14
  • 149
  • 175
Sizzles27
  • 94
  • 12

1 Answers1

1

The answer is that I had not changed the default idAttribute in my Backbone collection and model:

e.g.:

var SupplyCategoriesCollection = Backbone.Collection.extend({

    model: SupplyCategory,  
    url: '/api/products/',
    "idAttribute: "_id",

 }); 
Sizzles27
  • 94
  • 12