gpt4 book ai didi

ember.js - ember 数据未找到属性名称的模型

转载 作者:行者123 更新时间:2023-12-02 21:13:30 25 4
gpt4 key购买 nike

我定义了一个模型(app/models/job.js)

import DS from 'ember-data';

export default DS.Model.extend({
status: DS.attr(),
result: DS.attr()
});

我正在尝试从索引 Controller (app/controllers/index.js)加载它

import Ember from 'ember';

export default Ember.Controller.extend({
productName: "",
customerName: "",
startDate: "",
endDate: "",
actions: {
search: function() {
let data = this.store.find("job", '9e5ce869-89b3-4bfc-a70f-034593c21eae');
return data;
}
}
});

我得到的 HTTP 响应是:

{
"status": "OK",
"result": {
"b": 2,
"a": 2,
"see": 1,
"c": 1
}
}

我如何收到以下错误和警告:

WARNING: Encountered "status" in payload, but no model was found for model name "status" (resolved model name using next-gen-analytics@serializer:job:.modelNameFromPayloadKey("status"))
WARNING: Encountered "result" in payload, but no model was found for model name "result" (resolved model name using next-gen-analytics@serializer:job:.modelNameFromPayloadKey("result"))
TypeError: Cannot read property '_internalModel' of undefined
at finders.js:50
at Object.Backburner.run (ember.debug.js:224)
at ember$data$lib$system$store$$Service.extend._adapterRun (store.js:2043)
at finders.js:45
at tryCatch (ember.debug.js:56151)
at invokeCallback (ember.debug.js:56166)
at publish (ember.debug.js:56134)
at ember.debug.js:32577
at Queue.invoke (ember.debug.js:910)
at Object.Queue.flush (ember.debug.js:974)onerrorDefault @ ember.debug.js:32616exports.default.trigger @ ember.debug.js:56792Promise._onerror @ ember.debug.js:57758publishRejection @ ember.debug.js:56065(anonymous function) @ ember.debug.js:32577Queue.invoke @ ember.debug.js:910Queue.flush @ ember.debug.js:974DeferredActionQueues.flush @ ember.debug.js:770Backburner.end @ ember.debug.js:160Backburner.run @ ember.debug.js:228run @ ember.debug.js:20238ember$data$lib$system$adapter$$default.extend.ajax.Ember.RSVP.Promise.hash.success @ rest-adapter.js:831jQuery.Callbacks.fire @ jquery.js:3148jQuery.Callbacks.self.fireWith @ jquery.js:3260done @ jquery.js:9314jQuery.ajaxTransport.send.callback @ jquery.js:9718

如有任何建议,我们将不胜感激

更新我认为这是一个错误,所以我继续在 ember-data github 存储库上记录错误,大约一个小时内得到了 @wecc 的回复(很好) https://github.com/emberjs/data/issues/3683

为了解决这个问题,我编写了自己的序列化器。

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
normalizePayload: function(payload) {
return {
'job': {
id: '9e5ce869-89b3-4bfc-a70f-034593c21eae',
status: payload.status,
result: payload.result
}
};

}
});

现在,它开始工作了。

意见 我可以想到为什么他们以这种方式实现默认的 RESTSerializer,但我们可能应该向用户提供有关 ember-data 文档的更多信息,否则,尝试的新手使用它会迷路。

最佳答案

1) 您的服务器响应应该有一个与模型同名的根元素。因此,对于单个对象,它应该是:

{
"job": {
"id": "<backend must provide an id, integer preferred>"
"status": "OK",
"result": {
"b": 2,
"a": 2,
"see": 1,
"c": 1
}
}

对于多个对象:

{
"jobs": [{
"id": "<backend must provide an id, integer preferred>"
"status": "OK",
"result": {
"b": 2,
"a": 2,
"see": 1,
"c": 1
}, {/*next object*/}]
}

关于ember.js - ember 数据未找到属性名称的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com