2014-08-27 20 views
5

Tôi đang cố gắng để tạo ra một ứng dụng sử dụng ember.js và ember dữ liệu, sử dụng các phiên bản sau:lỗi khi xử lý lỗi tuyến đường trong ember.js với ember dữ liệu

DEBUG: Ember  : 1.7.0 
DEBUG: Ember Data : 1.0.0-beta.9 
DEBUG: Handlebars : 1.2.1 
DEBUG: jQuery  : 2.1.0 

Tôi đang sử dụng RESTAdapter để kết nối với một api tôi đã viết bằng cách sử dụng node.js.

Ngay sau khi tôi tải các ứng dụng tôi tiếp tục nhận được lỗi sau:

Error while processing route: students undefined is not a function TypeError: undefined is not a function 
    at http://localhost:9000/scripts/vendor/ember-data.js:12006:34 
    at tryCatch (http://localhost:9000/scripts/vendor/ember.js:45818:16) 
    at invokeCallback (http://localhost:9000/scripts/vendor/ember.js:45830:17) 
    at publish (http://localhost:9000/scripts/vendor/ember.js:45801:11) 
    at http://localhost:9000/scripts/vendor/ember.js:29069:9 
    at DeferredActionQueues.invoke (http://localhost:9000/scripts/vendor/ember.js:634:18) 
    at Object.DeferredActionQueues.flush (http://localhost:9000/scripts/vendor/ember.js:684:15) 
    at Object.Backburner.end (http://localhost:9000/scripts/vendor/ember.js:147:27) 
    at Object.Backburner.run (http://localhost:9000/scripts/vendor/ember.js:202:20) 
at apply (http://localhost:9000/scripts/vendor/ember.js:18382:27) 

Dưới đây là đoạn code tôi đang sử dụng (nạp theo thứ tự tôi dán nó):

ứng dụng. js

var App = window.App = Ember.Application.create({ 
    LOG_ACTIVE_GENERATION: true, 
    LOG_TRANSITIONS: true, 
    LOG_TRANSITIONS_INTERNAL: false, 
    LOG_VIEW_LOOKUPS: true 
}); 

store.js

App.ApplicationAdapter = DS.RESTAdapter.extend({ 
    host: 'http://localhost:3000', 

    serializer: DS.RESTSerializer.extend({ 
     primaryKey: function(type) { 
      return '_id'; 
     }, 

     serializeId: function(id) { 
      return id.toString(); 
     } 
    }) 
}); 

mô hình/student.js

App.Student = DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    nationality: DS.attr('string'), 
    createdAt: DS.attr('date') 
}); 

tuyến/app_route.js

App.StudentsRoute = Ember.Route.extend({ 
    model: function() { 
     return this.store.find('student'); 
    } 
}); 

router.js

App.Router.map(function() { 
    this.resource('students', {path: '/'}); 
}); 
.210

Và sau đây là phản ứng của API:

{ 
    students: [ 
     { 
      nationality: "Lorem", 
      lastName: "Doe", 
      firstName: "John", 
      _id: "53f87200f3750319b4791235", 
      createdAt: "2014-08-23T10:50:40.661Z" 
     }, 
     { 
      nationality: "Lorem", 
      lastName: "Doe", 
      firstName: "John", 
      _id: "53f87299f3750319b4791234", 
      createdAt: "2014-08-23T10:50:40.661Z" 
     } 
    ] 
} 

Dường như các cửa hàng không tải dữ liệu từ API, nhưng định dạng dữ liệu JSON có vẻ tốt đẹp. Bất kỳ ý tưởng về những gì có thể là sai?

Cảm ơn!

+0

Tôi nghĩ rằng tôi đã có lỗi tương tự chính xác, mà bắt đầu khi tôi đã không sử dụng 1.7.0 nhưng 1.8.0 -beta.1 ... chuyển về 1.7.0 để sửa nó cho tôi. Bạn không chắc chắn lý do tại sao bạn gặp sự cố: ( – Leeft

+0

Tôi tin rằng bạn cần sử dụng chuỗi trong 'primaryKey' thay vì hàm [dựa trên tài liệu] (http://emberjs.com/api/data/classes/DS .RESTSerializer.html # property_primaryKey) Một cái gì đó như 'primaryKey: ''. –

Trả lời

3

Vì vậy, sau khi tìm kiếm thêm về Stack Overflow, tôi đã tìm ra rằng serializer có tại được trong một lớp riêng biệt so với RESTAdapter, vì vậy đoạn code làm việc như sau:

store.js

App.ApplicationAdapter = DS.RESTAdapter.extend({ 
    host: 'http://localhost:3000' 
}); 

App.ApplicationSerializer = DS.RESTSerializer.extend({ 
    primaryKey: '_id', 
    serializeId: function(id) { 
     return id.toString(); 
    } 
}); 
+0

Đối với những người sử dụng 'ember-cli', hãy xem câu trả lời của tôi. –

0

Tôi gặp lỗi này và không liên quan gì đến bất kỳ nghi phạm thông thường nào.

Trong coffeescript, tôi đã bắt đầu xác định một mô hình.

App.Cost = DS.Model.extend 
    amount:   DS.attr 'number' 
    active:   DS.attr 'boolean' 

Để tạo mô hình thứ hai, tôi c/p mô hình đầu tiên của tôi trong và xóa các thuộc tính:

App.Cost = DS.Model.extend 

Các quay trở lại và cố gắng để chạy một mô hình dường như không liên quan

localhost:3000/products 

Kết quả là lỗi

Error while processing route: products.index 

Chỉ cần đảm bảo mô hình của tôi được đặt tên đúng cách đã giải quyết được lỗi:

App.Cost = DS.Model.extend(...) 
App.Price = DS.Model.extend(...) <- instead of repeating the Cost model 

Điều này có thể sản xuất lại được, vì vậy tôi nghĩ nó có thể hữu ích cho người khác.

2

Đây là câu trả lời được cập nhật cho những người đang sử dụng ember-cli.

ember g adapter application #=> creates app/adapters/application.js 
ember g serializer application #=> creates app/serializers/application.js 

Trong app/adapters/application.js:

import DS from 'ember-data'; 

export default DS.RestAdapter.extend({ 
    host: 'http://localhost:3000' 
}); 

Trong app/serializers/application.js:

import DS from 'ember-data'; 

export default DS.RESTSerializer.extend({ 
    primaryKey: '_id', 
    serializeId: function(id) { 
    return id.toString(); 
    } 
}); 
Các vấn đề liên quan