emberjs-处理从ajax调用返回的数据

emberjs-处理从ajax调用返回的数据,ajax,ember.js,Ajax,Ember.js,我相信这很简单,但是 我有一个ember路由,它使用Ajax调用来检索数据数组。我想从该数组创建一个模型对象数组 当我尝试创建相关模型的实例时,我得到了一个错误 Cannot read property '_attributes' of null TypeError: Cannot read property '_attributes' of null 为了尝试定义问题所在,我创建了两个独立于Ajax调用返回的数据的模型实例,例如: var testPccB = ParentCostCentr

我相信这很简单,但是

我有一个ember路由,它使用Ajax调用来检索数据数组。我想从该数组创建一个模型对象数组

当我尝试创建相关模型的实例时,我得到了一个错误

Cannot read property '_attributes' of null TypeError: Cannot read property '_attributes' of null
为了尝试定义问题所在,我创建了两个独立于Ajax调用返回的数据的模型实例,例如:

var testPccB = ParentCostCentre.create({cceClientCode : "ABC" });
错误也发生在那里

整个路线代码如下所示:

import Ember from 'ember';
import ParentCostCentre from "../models/parentcostcentre";

export default Ember.Route.extend({

    model() {
        return Ember.RSVP.hash({

            costcentres: this.store.findAll('costcentre'),

            parentcostcentres: this.testFindParents(),

        })
    },

    testFindParents: function () {
        var result = [];
        return new Ember.RSVP.Promise(function (resolve, reject) {

            const theDrvId = 888;
            const theClientCode = 'ABC';
            const theCceIdentifier = 'XYZXY'; 

            console.log("About to create testPccA");
            //this works
            var testPccA = ParentCostCentre.create();
            console.log("Finished create testPccA");
            console.log("About to create testPccB");
            //this generates the error
            var testPccB = ParentCostCentre.create({cceClientCode : "ABC" });
            console.log("Finished create testPccB");

            var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

            Ember.$.ajax({
                type: 'GET',
                url: theUrl,
                success: function (data) {
                    data.forEach(function (item) {
                        result.push(ParentCostCentre.create(item));
                    });
                    resolve(result);
                },
                error: function (request, textStatus, error) {
                    console.log(error);
                    reject(error);
                }
            });
        });
    },     

    setupController(controller, model) {
            controller.set('costcentres', model.costcentres);
            controller.set('parentcostcentres', model.parentcostcentres);
    }
});
testFindParents: function () {
    var result = [];
    return new Ember.RSVP.Promise(function (resolve, reject) {

        const theDrvId = 888;
        const theClientCode = 'ABC';
        const theCceIdentifier = 'XYZXY'; 


        var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

        Ember.$.ajax({
            type: 'GET',
            url: theUrl,
            success: function (data) {
                resolve(data);
            },
            error: function (request, textStatus, error) {
                console.log(error);
                reject(error);
            }
        });
    });
},     
有没有什么我没能做到的,可以让这一切顺利进行


编辑1:

这就是
parentcostcenter
模型的外观:

import DS from 'ember-data';

export default DS.Model.extend({
  cceClientCode: DS.attr('string'),
  cceIdentifier: DS.attr('string'),
  cciActiveFrom: DS.attr('date'),
  cciActiveTo: DS.attr('date'),
  cciAutoid: DS.attr('number'),
  cciCcGeoLevel: DS.attr('number'),
  cciCceId: DS.attr('number'),
  cciDescription: DS.attr('string'),
  cciPraId: DS.attr('number'),
  cciMatEmpId: DS.attr('number'),
  cciIsDisabled: DS.attr('number'),
  cciPostsummFlag: DS.attr('string'),
  cciTdEmpId: DS.attr('number'),
  emiActiveToPra: DS.attr('date'),
  emiActiveToTd: DS.attr('date'),
  emiEmailAddressPra: DS.attr('string'),
  emiEmailAddressTd: DS.attr('string'),
  emiNameFamilyPra: DS.attr('string'),
  emiNameFamilyTd: DS.attr('string'),
  emiNameFirstPra: DS.attr('string'),
  emiNameFirstTd: DS.attr('string')
});

编辑2 下面显示了API调用返回的数据。我不确定这有多重要,因为即使是这样的处理

var testPccB=parentcostcenter.create({cceClientCode:“ABC”})

。。。生成错误,但为了完整性,我将其包括在内

[
  {
    "id": 5101,
    "cceClientCode": "ABC",
    "cceIdentifier": "XYZXY",
    "cciAutoid": 81415,
    "cciCceId": 5111,
    "cciActiveFrom": "2017-03-27T11:47:23",
    "cciActiveTo": "2300-01-01T00:00:00",
    "cciGeoId": 888,
    "cciIsDisabled": 0,
    "cciPraEmpId": 40336,
    "cciTdEmpId": 14694,
    "cciDescription": "South Bigtown",
    "cciPostsummFlag": "S",
    "cciCcFinancialLevel": 1,
    "emiNameFirstPra": "Phil",
    "emiNameFamilyPra": "Franklin",
    "emiActiveToPra": "2300-01-01T00:00:00",
    "emiEmailAddressPra": "Phil.Franklin@example.com",
    "emiNameFirstTd": "Phillipa",
    "emiNameFamilyTd": "Howard",
    "emiActiveToTd": "2300-01-01T00:00:00",
    "emiEmailAddressTd": "Phillipa.Howard@example.com"
  }
]

好的,我找到了答案

我只是直接将来自Ajax的响应传递回来,而不是试图用它构建一个数组。因此,
testFindParents
代码现在如下所示:

import Ember from 'ember';
import ParentCostCentre from "../models/parentcostcentre";

export default Ember.Route.extend({

    model() {
        return Ember.RSVP.hash({

            costcentres: this.store.findAll('costcentre'),

            parentcostcentres: this.testFindParents(),

        })
    },

    testFindParents: function () {
        var result = [];
        return new Ember.RSVP.Promise(function (resolve, reject) {

            const theDrvId = 888;
            const theClientCode = 'ABC';
            const theCceIdentifier = 'XYZXY'; 

            console.log("About to create testPccA");
            //this works
            var testPccA = ParentCostCentre.create();
            console.log("Finished create testPccA");
            console.log("About to create testPccB");
            //this generates the error
            var testPccB = ParentCostCentre.create({cceClientCode : "ABC" });
            console.log("Finished create testPccB");

            var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

            Ember.$.ajax({
                type: 'GET',
                url: theUrl,
                success: function (data) {
                    data.forEach(function (item) {
                        result.push(ParentCostCentre.create(item));
                    });
                    resolve(result);
                },
                error: function (request, textStatus, error) {
                    console.log(error);
                    reject(error);
                }
            });
        });
    },     

    setupController(controller, model) {
            controller.set('costcentres', model.costcentres);
            controller.set('parentcostcentres', model.parentcostcentres);
    }
});
testFindParents: function () {
    var result = [];
    return new Ember.RSVP.Promise(function (resolve, reject) {

        const theDrvId = 888;
        const theClientCode = 'ABC';
        const theCceIdentifier = 'XYZXY'; 


        var theUrl = "api/parentcostcentres/" + theDrvId + "/" + theClientCode + "/" + theCceIdentifier ;

        Ember.$.ajax({
            type: 'GET',
            url: theUrl,
            success: function (data) {
                resolve(data);
            },
            error: function (request, textStatus, error) {
                console.log(error);
                reject(error);
            }
        });
    });
},     

当然,这并不能解释为什么我不能像在测试代码中尝试的那样实例化
parentcostcenter
的实例,但主要问题至少得到了解决。

因此,在创建parentcostcenter实例时会发生错误。因此,发布您的
模型/parentcostcenter.js
@Enno可能会有所帮助-感谢您的回复。我正要编辑这个问题,把它包括进去。