无法读取空烬的属性'id'
问题描述:
我试图从一个带有Ember应用程序的WordPress应用程序中获取数据。我试图在ID为'2'的WordPress中获取菜单项。我使用的是ember-wordpress插件,可以成功获取我的帖子,我只是努力从API获取菜单。无法读取空烬的属性'id'
下面是我的菜单模式
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
count: DS.attr('number')
});
这里是我的路线。
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
menu: this.store.findRecord('menu', 2)
});
}
});
然而,当我尝试打网址我得到的错误在我的控制台
Error while processing route: Cannot read property 'id' of null TypeError: Cannot read property 'id' of null
这是我从我的终点得到JSON。
{
"ID": 2,
"name": "Main Menu",
"slug": "main-menu",
"description": "",
"count": 2,
"items": [
{
"id": 19,
"order": 1,
"parent": 0,
"title": "Projects",
"url": "http://localhost:8888/ember-wp/projects/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 17,
"object": "page",
"object_slug": "projects",
"type": "post_type",
"type_label": "Page"
},
{
"id": 20,
"order": 2,
"parent": 0,
"title": "Services",
"url": "http://localhost:8888/ember-wp/services/",
"attr": "",
"target": "",
"classes": "",
"xfn": "",
"description": "",
"object_id": 15,
"object": "page",
"object_slug": "services",
"type": "post_type",
"type_label": "Page"
}
],
"meta": {
"links": {
"collection": "http://localhost:8888/ember-wp/wp-json/wp/v2/menus/",
"self": "http://localhost:8888/ember-wp/wp-json/wp/v2/menus/2"
}
}
}
任何人都可以看到我要去哪里吗?
答
我想象你正在使用WP API Menus插件来下拉数据,因为我遇到了同样的问题。您将需要编辑该插件中的某个文件来纠正此问题。
在wp-api-menus-v2.php
...
- 108上,改变
$rest_menus[ $i ]['ID'] = $menu['term_id'];
到$rest_menus[ $i ]['id'] = $menu['term_id'];
- 线143,改变
$rest_menu['ID'] = abs($menu['term_id']);
到$rest_menu['id'] = abs($menu['term_id']);
从大写更改这两个键小写将解决你的问题。
答
我不知道你得到的错误,但第一个元素没有'id'字段而是'ID'字段(大写)。