未设置模型属性

Model properties are not set

我正在尝试从服务器获取数据并将其设置为 Ember 的 DS.Model(通过 ember 的魔法)。但是创建的记录没有数据。

我有一个模型models/product.js:

const { Model } = DS;

export default Model.extend({
    name: DS.attr(),
    price: DS.attr()
});

在routes/product.js中我请求所有产品:

    model() {
        return this.store.findAll('product');
    }

服务器returns数据: https://gyazo.com/ba38b756f334bc22d07fe18ccfddda34

我希望 ember 使用来自服务器的数据创建 3 条记录。 但实际上,它创造了这个 https://gyazo.com/a9a7b77d838ec33b05e5f81ef8304cdb

怎么了?我想我不应该指定任何适配器和序列化器来获得默认的 Ember 行为。

您的 API 不遵循 JSON:API specification 但 Ember 数据默认使用那个。如果您不遵循该约定,则需要更改应用程序序列化程序和适配器。让我引用 Ember 文档:

Ember Data flexibility

Thanks to its use of the adapter pattern, Ember Data can be configured to work with many different kinds of backends. There is an entire ecosystem of adapters and several built-in adapters that allow your Ember app to talk to different types of servers.

By default, Ember Data is designed to work out of the box with JSON:API. JSON:API is a formal specification for building conventional, robust, and performant APIs that allow clients and servers to communicate model data.

JSON:API standardizes how JavaScript applications talk to servers, so you decrease the coupling between your frontend and backend, and have more freedom to change pieces of your stack.

If you need to integrate your Ember.js app with a server that does not have an adapter available (for example, you hand-rolled an API server that does not adhere to any JSON specification), Ember Data is designed to be configurable to work with whatever data your server returns.

Source: https://guides.emberjs.com/release/models/#toc_ember-data-flexibility

我不确定这是一个问题还是规范性陈述,但实际上如果您的 API 不是 JSON:API.

,您必须指定适配器和序列化程序