【问题标题】:Error: Assertion Failed: You must include an 'id' for member in an object passed to 'push'错误:断言失败:您必须在传递给“推送”的对象中包含成员的“id”
【发布时间】:2018-03-30 22:19:04
【问题描述】:

我在 Stackoverflow 上查看了这个问题,但仍然无法弄清楚如何摆脱这个错误。基本上,我正在尝试使用 Wordpress API 并将其连接到我的 Ember 应用程序,方法是按照本教程 https://www.codesandnotes.com/ember-js/ember-wordpress/

我收到此错误:

Error Screenshot

我无法弄清楚它为什么会出现或如何摆脱它。我想不出任何地方/其他人可以问,所以任何帮助都会非常感激。 这是我的代码:

app/router.JS

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('about');
  this.route('members');
});

export default Router;

app/routes/member.js

import Ember from 'ember';

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

app/models/member.js

import DS from 'ember-data';
import PostModel from 'ember-wordpress/models/post';

export default PostModel.extend({

});

app/config/environment.js

/* eslint-env node */
'use strict';

module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'doe-site',
    environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    },
    wordpressHost: 'http://wordpress.daughtersofeve.org/'
  };

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {

  }

  return ENV;
};

app/package.json

{
  "name": "doe-site",
  "version": "0.0.0",
  "private": true,
  "description": "Small description for doe-site goes here",
  "license": "MIT",
  "author": "",
  "directories": {
    "doc": "doc",
    "test": "tests"
  },
  "repository": "",
  "scripts": {
    "build": "ember build",
    "start": "ember server",
    "test": "ember test"
  },
  "devDependencies": {
    "broccoli-asset-rev": "^2.4.5",
    "ember-ajax": "^3.0.0",
    "ember-cli": "~2.15.1",
    "ember-cli-app-version": "^3.0.0",
    "ember-cli-babel": "^6.3.0",
    "ember-cli-dependency-checker": "^2.0.0",
    "ember-cli-eslint": "^4.0.0",
    "ember-cli-htmlbars": "^2.0.1",
    "ember-cli-htmlbars-inline-precompile": "^1.0.0",
    "ember-cli-inject-live-reload": "^1.4.1",
    "ember-cli-qunit": "^4.0.0",
    "ember-cli-shims": "^1.1.0",
    "ember-cli-sri": "^2.1.0",
    "ember-cli-uglify": "^1.2.0",
    "ember-data": "~2.15.0",
    "ember-export-application-global": "^2.0.0",
    "ember-load-initializers": "^1.0.0",
    "ember-resolver": "^4.0.0",
    "ember-source": "~2.15.0",
    "ember-welcome-page": "^3.0.0",
    "ember-wordpress": "^0.1.7",
    "loader.js": "^4.2.3"
  },
  "engines": {
    "node": "^4.5 || 6.* || >= 7.*"
  }
}

【问题讨论】:

  • 该错误听起来像是您遇到的问题是您收到的有效负载未正确转换为 Ember Data 预期的内容。您可以发布您的服务器通过网络发回的内容吗?

标签: javascript ember.js wordpress-rest-api


【解决方案1】:

如果您的服务器旁加载您的应用程序未知类型的记录,我已经看到 ember-data 2.15 的类似异常。

快速示例:

// response body
{
  "data": {
    "type": "foos",
    "id": "1",
    "relationships": {
      "bar": {
        "data": {
          "id": "1",
          "type": "bars"
        }
      }
    },
  },
  "included": [
    {
      "type": "bars",
      "id": "1"
    }
  ]
}

如果您有一个模型 foo 但没有模型 bar 它会抛出。我还没有时间测试它是否已在最新版本中修复,如果没有则创建错误报告。

如果您不是这种情况,请包括响应正文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-18
    • 2023-03-17
    • 1970-01-01
    • 2014-10-21
    • 2016-07-19
    • 1970-01-01
    • 2016-12-30
    • 2018-09-18
    相关资源
    最近更新 更多