加载路径时出现ember-data错误:TypeError {}

时间:2013-10-17 12:31:30

标签: javascript ember.js ember-data

我最近开始在我的项目中实现ember数据库,但现在一切都坏了,谷歌Chrome控制台为我提供了这些错误:

- Error while loading route: TypeError {} 
- Uncaught TypeError: Cannot read property 'id' of undefined 
- Port: Could not establish connection. Receiving end does not exist.

我要打开的网址是:/#/ track / 85

这是相关代码:

Shoutzor = Ember.Application.create();
Shoutzor.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: '/api/emberstore',
    host: 'http://www.shoutzor.nl'
});

var attr = DS.attr,
    hasMany = DS.hasMany,
    belongsTo = DS.belongsTo;

Shoutzor.Track = DS.Model.extend({
    id: attr('number'),
    title: attr('string'),
    //length: attr('number'),
    //artist: hasMany('artist'),
    //album: hasMany('album'),

    /* Convert the length in seconds to a string like '01:55' */
    convertedLength: function() {
        var sec_num = parseInt(this.get('length'), 10); // don't forget the second parm
        var hours   = Math.floor(sec_num / 3600);
        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        var seconds = sec_num - (hours * 3600) - (minutes * 60);

        if (hours   < 10 && hours > 0) {hours   = "0"+hours;}
        if (minutes < 10 && minutes > 0) {minutes = "0"+minutes;}
        if (seconds < 10) {seconds = "0"+seconds;}
        var time    = ((hours != 0) ? hours + ':' : '') + ((minutes != 0) ? minutes +':' : '') + seconds;

        return time;
    }.property('length')
});

Shoutzor.Album = DS.Model.extend({
    id: attr('number'),
    artist: belongsTo('artist'),
    title: attr('string'),
    cover: attr('string')
});

Shoutzor.Artist = DS.Model.extend({
    id: attr('number'),
    name: attr('string'),
    profileimage: attr('string')
});

Shoutzor.Router.map(function() {
    //Track Page
    this.route("track", { path: "/track/:id" });
});

Shoutzor.TrackRoute = Ember.Route.extend({
    setupController: function(controller) {
        controller.set('pageTitle', "Track");
    },

    renderTemplate: function() {
        this.render('TrackContent', { outlet: 'pageContent', into: 'application' });
    },

    model: function(params) {
        return this.store.find('track', params.id);
    }
});

Shoutzor.TrackController = Ember.Controller.extend();

Shoutzor.TrackView = Ember.View.extend();

我的API提供了如下响应:

{"tracks":{"id":85,"title":"Untitled","file":{"filename":"Lines of Latitude - You want it (Free Download).mp3","filepath":"\/home\/vhosts\/domains\/shoutzor\/music\/Lines of Latitude - You want it (Free Download).mp3","crc":"51ca8346","length":262,"id3":[]},"artist":[],"album":[]}}

我一直在查看多个SO帖子和谷歌搜索结果,但没有一个解决了我的问题(或者我正在寻找错误的东西),

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您不需要在模型类中包含id: DS.attr()。此外,单个资源的响应应该具有根密钥track而不是tracks

{"track":{"id":85,"title":"Untitled","file":{"filename":"Lines of Latitude - You want it (Free Download).mp3","filepath":"\/home\/vhosts\/domains\/shoutzor\/music\/Lines of Latitude - You want it (Free Download).mp3","crc":"51ca8346","length":262,"id3":[]},"artist":[],"album":[]}}`