加载路由时发生ember-data错误:TypeError {} [英] ember-data error while loading route: TypeError {}

查看:92
本文介绍了加载路由时发生ember-data错误:TypeError {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

   - 加载路由时出错:TypeError {} 
- 未捕获TypeError:无法读取未定义的属性'id'
- 端口:无法建立连接。接收端不存在。

我试图打开的URL是:/#/ 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'),
//艺术家:hasMany('artist'),
//专辑:hasMany('album'),

/ *将长度以秒为单位转换为'01:55'的字符串* /
convertedLength:function(){
var sec_num = parseInt(this.get('length'),10); // don不要忘记第二个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&&分钟> 0){minutes =0+分钟;}
if(seconds< 10){seconds =0+ seconds;}
var time =((hours!= 0)?hours +':':'')+((minutes!= 0)?minutes +':':'')+秒;

返回时间;
} .property('length')
});

Shoutzor.Album = DS.Model.extend({
id:attr('number'),
艺术家:belongsTo('艺术家'),
标题:attr('string'),
cover:attr('string')
});

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

Shoutzor.Router.map(function(){
//跟踪页
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提供了一个响应,如:

  {track:{id:85,title:Untitled,file:{filename: ).mp3,filepath:\ / home\ / vhosts\ / domains\ / shoutzor\ / music\ /纬度线 - 你想要它(免费下载).mp3,crc :51ca8346,length:262,id3:[]},artist:[],album:[]}} 

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



非常感谢任何帮助!

解决方案

您不需要包含<$您的模型类中的c $ c> id:DS.attr()另外,单一资源的响应应该有根密钥 track 而不是 track

  {track:{id:85,title:Untitled,file:{filename纬度 - 你想要的(免费下载).mp3,文件路径:\ / home\ / vhosts\ / domains\ / shoutzor\ / music\ /纬度线 - 你想要下载).mp3,crc:51ca8346,length:262,id3:[]},artist:[],album:[]}}`


I recently started to implement the ember data library into my project but now everything is broken, the google chrome console provides me with these errors:

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

The URL I'm trying to open is: /#/track/85

This is the relevant code:

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();

My API provides a response like:

{"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":[]}}

I have been looking at multiple SO posts and google search results but none of those solve my problem (or I'm looking for the wrong thing),

Any help is greatly appreciated!

解决方案

You don't need to include id: DS.attr() in your model class. Also, the response for a singular resource should have the root key track instead of 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":[]}}`

这篇关于加载路由时发生ember-data错误:TypeError {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆