JSON object being returned should be array

僤鯓⒐⒋嵵緔 提交于 2019-12-11 06:29:31

问题


I am using backbone and my fetch was not returning anything (when I tried to iterate with each), I had a close look and found that it fires a get request and this is what is being returned

{
    "-JA2Ey5xJvV-BfvgmzT3": {
        "date": "10-23-33",
        "title": "le shmow",
        "content": "testing lez post!"
    },
    "-JA2Ey6FwK5eK9mELCnd": {
        "date": "10-23-33",
        "title": "test post",
        "content": "testing lez post!"
    }
}

According to backbones documentation:

The server handler for fetch requests should return a JSON array of models., the chrome dev toolkit has these labelled as objects? So could this be the problem?

EDIT backbone docs state:

Fetch the default set of models for this collection from the server, setting them on the collection when they arrive. The options hash takes success and error callbacks which will both be passed (collection, response, options) as arguments. When the model data returns from the server, it uses set to (intelligently) merge the fetched models, unless you pass {reset: true}, in which case the collection will be (efficiently) reset. Delegates to Backbone.sync under the covers for custom persistence strategies and returns a jqXHR. The server handler for fetch requests should return a JSON array of models.

    console.log(posts.fetch());

    posts.fetch();

    console.log(posts);

    posts.each(function(post) {
        console.log(post.get("title"));
    });

The first console log displays a set of "objects", with the correct data etc, the second one is labelled r (no idea what that means?) and only has a length of 0 and I can't see the data within it.

When I used each to iterate over it, I get nothing

EDIT: got it working(sort of), still have no idea why i need this hack for what should be a straightforward operation...perhaps im doing something seriously wrong. Is there anyway I could edit the parse function so I just have to type post.title instead of post.attributes.title

 var Posts = Backbone.Collection.extend({
    model: Post,
    url: base_url + '/posts.json',
      parse: function(response) {
        return _.map(response, function(model, key) {
          //i'm assuming the key of each object is the id of the model
          model['id'] = key;
          return model;
        });
      }
});

posts = new Posts;

posts.fetch().complete(function() {
    posts.each(function(post) {
        console.log(post.attributes.title);
    });
});

回答1:


As the document states, your server has to return an array of models in JSON format, in order to make your collection.fetch() work.

if you cannot change the server response, you will want to use parse method to convert the response to an array.

so in your collection definition:

YourCollection = Backbone.Collection.extend({

  parse: function(response) {
    return _.map(response, function(model, key) {
      //i'm assuming the key of each object is the id of the model
      model['id'] = key;
      return model;
    });
  }
});

for more information about parse, see: http://backbonejs.org/#Collection-parse



来源:https://stackoverflow.com/questions/20404664/json-object-being-returned-should-be-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!