Sizzles27
Sizzles27

Reputation: 94

Render collection data from mongo db in node application

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.

Upvotes: 0

Views: 228

Answers (1)

Sizzles27
Sizzles27

Reputation: 94

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",

 }); 

Upvotes: 1

Related Questions