Ember错误:断言失败:`id`传递给`findRecord()`必须是非空字符串或数字

时间:2016-08-20 02:51:43

标签: javascript ember.js

Router.js

Router.map(function() {
  this.route('artists', function() {
    this.route('artist', {path: ':id'}, function() {
      this.route('songs');
    });
  });
});

路由/ artists.js

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

路由/艺术家/ artist.js

export default Ember.Route.extend({
    model(params){
        console.log(params.id);  //printed a valid id (-KP_nH0EMk3zYqhiWJZ1)
        return this.store.findRecord(params.id);
    }
});

模板/ artists.hbs

{{#each model as |artist|}}
    <li class="list-group-item"> {{#link-to 'artists.artist.songs' artist}} {{artist.name}} {{/link-to}}</li>
{{/each}}

错误

ember.debug.js:28535 Error while processing route: artists.artist.songs Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number Error: Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:26708:21)
    at assert (http://localhost:4200/assets/vendor.js:16548:13)
    at Object.assert (http://localhost:4200/assets/vendor.js:26472:34)
    at assert (http://localhost:4200/assets/vendor.js:68333:37)
    at Class.findRecord (http://localhost:4200/assets/vendor.js:76157:41)
    at Class.model (http://localhost:4200/assets/ember-basics.js:259:22)
    at Class.deserialize (http://localhost:4200/assets/vendor.js:37088:19)
    at Object.applyHook (http://localhost:4200/assets/vendor.js:63718:30)
    at Object.runSharedModelHook (http://localhost:4200/assets/vendor.js:61926:33)logError @ ember.debug.js:28535error @ ember.debug.js:28478triggerEvent @ ember.debug.js:28594trigger @ ember.debug.js:53473trigger @ ember.debug.js:53287(anonymous function) @ ember.debug.js:53107tryCatch @ ember.debug.js:53806invokeCallback @ ember.debug.js:53821publish @ ember.debug.js:53789publishRejection @ ember.debug.js:53724(anonymous function) @ ember.debug.js:32054invoke @ ember.debug.js:333flush @ ember.debug.js:397flush @ ember.debug.js:205end @ ember.debug.js:560run @ ember.debug.js:682run @ ember.debug.js:21139(anonymous function) @ firebase.js:134c @ firebase.js:396(anonymous function) @ firebase.js:387Ub @ firebase.js:283vc @ firebase.js:271wc @ firebase.js:270(anonymous function) @ firebase.js:454(anonymous function) @ firebase.js:427h.wd @ firebase.js:432af.wd @ firebase.js:335(anonymous function) @ firebase.js:333yd @ firebase.js:286La.onmessage @ firebase.js:285
ember.debug.js:32096 Error: Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:26708:21)
    at assert (http://localhost:4200/assets/vendor.js:16548:13)
    at Object.assert (http://localhost:4200/assets/vendor.js:26472:34)
    at assert (http://localhost:4200/assets/vendor.js:68333:37)
    at Class.findRecord (http://localhost:4200/assets/vendor.js:76157:41)
    at Class.model (http://localhost:4200/assets/ember-basics.js:259:22)
    at Class.deserialize (http://localhost:4200/assets/vendor.js:37088:19)
    at Object.applyHook (http://localhost:4200/assets/vendor.js:63718:30)
    at Object.runSharedModelHook (http://localhost:4200/assets/vendor.js:61926:33)

导航到“artists / {id}”和“artists / {id} / songs”时,我遇到此错误。我尝试在艺术家/艺术家路线上打印params.id。我可以在控制台中看到有效的ID。谁能猜出可能出现的错误

1 个答案:

答案 0 :(得分:6)

findRecord需要两个必填参数modelNameid,在您的案例中,模型名称缺失。

<强>路由/艺术家/ artist.js

export default Ember.Route.extend({
    model(params){
        console.log(params.id);  //printed a valid id (-KP_nH0EMk3zYqhiWJZ1)
        return this.store.findRecord('artist',params.id);
    }
});

参考:http://emberjs.com/api/data/classes/DS.Store.html#method_findRecord