Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 我怎样才能避免;断言失败:`id`传递给`findRecord()`必须是非空字符串或数字";刷新余烬页面时?_Ember.js - Fatal编程技术网

Ember.js 我怎样才能避免;断言失败:`id`传递给`findRecord()`必须是非空字符串或数字";刷新余烬页面时?

Ember.js 我怎样才能避免;断言失败:`id`传递给`findRecord()`必须是非空字符串或数字";刷新余烬页面时?,ember.js,Ember.js,关于余烬有一个非常恼人的特性,我不知道该如何使用它。我可能有一个如下所示的url http://{my-blog-name}/posts/view/{some-blogpost-ID} 我进入此页面的方式是单击我的{my blog name}/posts页面内的链接。这将正常工作并按预期显示页面。但是,如果我尝试刷新页面,或者如果我只是在我的url搜索框中直接键入我的http://{my blog name}/posts/view/{some blog post ID},我将得到 Assert

关于余烬有一个非常恼人的特性,我不知道该如何使用它。我可能有一个如下所示的url

http://{my-blog-name}/posts/view/{some-blogpost-ID}
我进入此页面的方式是单击我的
{my blog name}/posts
页面内的链接。这将正常工作并按预期显示页面。但是,如果我尝试刷新页面,或者如果我只是在我的url搜索框中直接键入我的
http://{my blog name}/posts/view/{some blog post ID}
,我将得到

Assertion Failed: `id` passed to `findRecord()` has to be non-empty string or number
下面是我如何导航到
posts/view/{some blog id}
页面的

post.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('post');
    }
});
<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li> 
import Ember from 'ember';

var siteId;

export default Ember.Route.extend({
    model(params) {
        siteId = params.site_id;
        return this.store.findRecord('post', params.site_id);
    }
});
<div id="Links">
  <h1 id="blog-header-title">My Blog</h1>
  <!--<p>{{!#link-to 'welcome'}} See about me{{!/link-to}}</p>-->
  {{outlet}}
</div>

{{outlet}}
import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('index', { path: '/' }); // This is usually automatic if path undeclared, but declared here to support /index below
  this.route('posts', function() {
    this.route('view', {path: '/view/:post_id'});
  });
  this.route('welcome');
}
posts.hbs

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('post');
    }
});
<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li> 
import Ember from 'ember';

var siteId;

export default Ember.Route.extend({
    model(params) {
        siteId = params.site_id;
        return this.store.findRecord('post', params.site_id);
    }
});
<div id="Links">
  <h1 id="blog-header-title">My Blog</h1>
  <!--<p>{{!#link-to 'welcome'}} See about me{{!/link-to}}</p>-->
  {{outlet}}
</div>

{{outlet}}
import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('index', { path: '/' }); // This is usually automatic if path undeclared, but declared here to support /index below
  this.route('posts', function() {
    this.route('view', {path: '/view/:post_id'});
  });
  this.route('welcome');
}
view.hbs

import Ember from 'ember';

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('post');
    }
});
<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li> 
import Ember from 'ember';

var siteId;

export default Ember.Route.extend({
    model(params) {
        siteId = params.site_id;
        return this.store.findRecord('post', params.site_id);
    }
});
<div id="Links">
  <h1 id="blog-header-title">My Blog</h1>
  <!--<p>{{!#link-to 'welcome'}} See about me{{!/link-to}}</p>-->
  {{outlet}}
</div>

{{outlet}}
import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
  this.route('index', { path: '/' }); // This is usually automatic if path undeclared, but declared here to support /index below
  this.route('posts', function() {
    this.route('view', {path: '/view/:post_id'});
  });
  this.route('welcome');
}

这真的很令人沮丧,因为这意味着我不能写博客,也不能和朋友分享链接。为什么会发生这种情况?有没有一个好的方法来避免这种情况

posts.js路由返回所有可用的帖子,即
RecordArray

<li class="title-list-item">{{#link-to "posts.view" posts}}{{posts.title}}{{/link-to}}</li>
  • {{{{#链接到“posts.view”posts}{{posts.title}}{{/link to}
  • 所以在上面的
    帖子中
    -指单个帖子模型或帖子模型的RecordArray?。如果以上是单个模型,那么您将在view.js的模型钩子中收到
    params.post\u id
    ,当前您使用的是
    params.site\u id
    ,而不是
    params.post\u id


    不执行模型挂钩的原因。

    注意:具有动态管段的管线将始终具有其模型挂钩 通过URL输入时调用。如果路线是通过 过渡(例如,使用“链接到把手”辅助对象时)和 提供模型上下文(链接到的第二个参数),然后提供钩子 没有执行。如果提供了标识符(如id或slug) 然后将执行模型挂钩


    请在问题中加入
    router.js
    文件。并验证当您在link to helper中传递site_id as 123时是否正常?
  • {{{{posts.title}}{{/link to}}
  • 。此外,当我们不遵循余烬约定时,我相信这个问题会出现。很抱歉回复太晚。完成。还添加了post.js和view.hbs,希望它们能有所帮助。如果您还想查看更多文件,请告诉我。谢谢!这是有道理的。我会尝试在早上玩它,如果它是一个RecordArray问题,然后奖励最佳答案。+1。问题是“site_id”参数。这很有趣,但我现在很好奇,为什么即使我的参数命名错误,LinkTo仍然可以工作。
    {{{#链接到“posts.view”posts}}
    这里提供的是
    posts
    模型,因为它将跳过执行模型挂钩和setupController。所以实际上它并没有执行您编写的模型钩子。要执行模型钩子,您需要提供id。请参阅此处的动态段。谢谢kumkanillam,我只想说我在emberjs页面上看到了很多你的答案,我觉得它们总是非常有用。是的。我总是说它非常有用。感谢恩伯学习团队。