处理路由时出错:索引断言失败:您必须包含'id'

时间:2016-03-31 10:15:18

标签: json node.js ember.js ember-data

我正在制作一个城市仪表板的最终学位项目。我在用: - PostgreSQL作为DB - Node.js + Express.js + Massive.js作为服务器 - Ember.js作为客户端应用

实际上,我正在测试我是否可以从DB获取数据到Ember,但是我收到了下一个错误(我已经尝试了几乎我在这里找到的所有解决方案):

  

处理路由时出错:索引断言失败:您必须在传递给'push'的对象中包含poblacio的'id'错误:断言失败:您必须在传递给'push'的对象中包含poblacio的'id' “

这些是我的文件:

server.js (只是一块)

router.route('/poblacios')
        .get(function(request, response) {
            db.poblacio.find({}, function(err, res){
                response.json(res);
            });
        });

 app.use('/api/v1/', router);

适配器/ application.js中

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
    namespace: 'api/v1'
});

路线/ application.js中

import Ember from 'ember';

export default Ember.Route.extend({
    model() {
        return this.store.findAll('poblacio');
    }
});

模型/ poblacio.js

import DS from 'ember-data';

export default DS.Model.extend({
  anny: DS.attr('number'),
  districte: DS.attr('number'),
  barri: DS.attr('string')
});

串行器/ poblacio.js

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
    primaryKey: 'id',
    normalizeFindAllResponse(store, type, payload) {
        return {
            data: {
                id: payload.id,
                type: type.modelName,
                attributes: {
                    anny: payload.anny,
                    districte: payload.districte,
                    barri: payload.barri,
                }               
            }
        };
    }
});

localhost:3000 / api / v1 / poblacios 按地区,社区和年份返回0到95岁以上的女性和男性人数,每行的ID为:

[{"id":1,"anny":2015,"districte":1,"barri":"1. el Raval","donesanys0":206,"donesanys1":212,"donesanys2":206,"donesanys3":247....
{"id":2,"anny":2015,"districte":1,"barri":"2. el Barri Gotic","donesanys0":48,"donesanys1":53...
....
{"id":657,"anny":2007,"districte":10,"barri":"73. la Verneda i la Pau","donesanys0":103,"donesanys1":118,"donesanys2":123,"donesanys3":107...

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如果您使用 results = jsonResponse['responseData']['results'] TypeError: 'NoneType' object has no attribute '__getitem__' ,则需要关注JSON API spec

根据规范,上面的JSON应该类似于:

 import urllib
    import urllib2
    from urllib import urlencode
    import json as m_json
    from urllib2 import urlopen
    import re
    import json
    from nltk.corpus import stopwords
    import sys
    from urllib2 import urlopen
    import urllib2
    import simplejson
    import pprint

    words = ['headache','diabetes','myopia','dhaed','snow','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids','blindness','head','ache','acne','aids']


    for word in words:
     url = ('https://ajax.googleapis.com/ajax/services/search/web'
           '?v=1.0&q='+word+'&userip=192.168.1.105')
     request = urllib2.Request(url)
     response = urllib2.urlopen(request)
     jsonResponse=json.loads(response.read())
     #print "the response now is: ",jsonResponse
     #pprint.pprint(jsonResponse)
     results = jsonResponse['responseData']['results']
     for result in results:
      print "\nthe result is: ",result
      url =result['url']
      print "\nthe url is: ",url
      try:
       page=urllib2.urlopen(url).read()
      except urllib2.HTTPError,err:
       if err.code == 403:
        print "bad"
        continue
       else:
        print "good"
        break
      except urllib2.HTTPError:
        print "server error"
      except:
       print "dont know the error"

这就是问题所在,你的有效载荷是一个数组,因此有效的解决方案就是:

JSONAPIAdapter

希望这有帮助。