我正在尝试使用Ember应用程序从WordPress应用程序中获取数据。我试图在IDPress中获取ID为'2'的菜单项。我正在使用ember-wordpress插件并且可以成功获取我的帖子,我只是在努力从API中获取菜单。
以下是我的菜单型号
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)
});
}
});
但是,当我尝试点击URL时,我在控制台中收到错误
处理路由时出错:无法读取null的属性“id” TypeError:无法读取null
的属性“id”
这是我从终点得到的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"
}
}
}
有人能看到我在哪里错了吗?
答案 0 :(得分:1)
我不知道你得到的错误,但第一个元素没有' id'字段,但' ID' field(大写)。
答案 1 :(得分:1)
我想你正在使用WP API Menus插件来下载数据,因为我遇到了同样的问题。您需要编辑该插件中的一个文件才能解决此问题。
在wp-api-menus-v2.php
...
$rest_menus[ $i ]['ID'] = $menu['term_id'];
更改为$rest_menus[ $i ]['id'] = $menu['term_id'];
$rest_menu['ID'] = abs( $menu['term_id'] );
更改为$rest_menu['id'] = abs( $menu['term_id'] );
将这两个键从大写更改为小写将解决您的问题。