TypeError:object不是函数 - Javascript,ExtJS,Jasmine和TypeError:将循环结构转换为JSON

时间:2014-07-09 00:17:16

标签: javascript extjs jasmine

我是新手(对Jasmine测试,ExtJs和JS来说真的很新),我必须解决这个错误/错误。我正在运行一些单元测试,并且我不断收到以下错误:

TypeError: object is not a function
TypeError: object is not a function
at eval (eval at <anonymous> (...main/lib/ext/ext-4.2.0-gpl/ext-all-debug.js:5499:56),     <anonymous>:3:8)
at Object.Ext.ClassManager.instantiate (...main/lib/ext/ext-4.2.0-gpl/ext-all-debug.js:5471:53)
at Object.create (...main/lib/ext/ext-4.2.0-gpl/ext-all-debug.js:2110:39)
at Ext.define.urnSearchBasic (../../../../main/app/model/SecurityGatewayUrnSearchResponse.js:72:26)
at urnSearchBasic (...test/spec/model/SecurityGatewayUrnSearchResponseTest.js:68:27)
at null.<anonymous> (...test/spec/model/SecurityGatewayUrnSearchResponseTest.js:150:17)
at jasmine.Block.execute (...test/lib/jasmine/jasmine.js:1024:15)
at jasmine.Queue.next_ (...test/lib/jasmine/jasmine.js:2025:31)
at jasmine.Queue.start (...test/lib/jasmine/jasmine.js:1978:8)
at jasmine.Spec.execute (.../test/lib/jasmine/jasmine.js:2305:14)
timeout: timed out after 10000 msec waiting for passed === true

SecurityGatewanyUrnSeachResponse的代码(仅发布第61-84行的部分代码):

61 urnSearchBasic: function (basic, config) {
    config = Ext.apply({}, config);

    var proxy = Deft.ioc.Injector.resolve('securityGatewayUrnMultiSearchProxy'),
        urn = Ext.create('CAS.securityAdminUrnProv.model.Urn'),
        formWriter,
        createdProxy;
    urn.data.urn = basic;
    urn.data.pageNo = 0;
    urn.data.pageSize = this.configData.getUrnPageSize();

72  formWriter = Ext.create('CAS.securityAdminUrnProv.model.SearchFormWriter');
    formWriter.setUrn(urn.data);

    createdProxy = Ext.create(proxy.$className);
    createdProxy.url = createdProxy.url + '/basic';
    createdProxy.setApiUrl(createdProxy.url);
    createdProxy.headers = this.configData.getGatewayProxyHeaders();
    createdProxy.setWriter(formWriter);

    this.setProxy(createdProxy);

    this.save(config);
84 }, 

第72行导致问题(Ext.create(... SearchFormWriter))。 SearchFormWirter的代码如下:

Ext.define('CAS.securityAdminUrnProv.model.i SearchFormWriter', {
    extend: "Ext.data.Writer",
    alias: 'writer.SearchFormWriter',
    inject: [ 'configData' ],
    allowSingle: true,
    config: {
        urn: {}
    },

    writeRecords: function (request, data) {
        var cache = [];
            request.rawData = JSON.stringify(this.getUrn(), function(key, value) {
                if (typeof value === 'object' && value !== null) {
                    if (cache.indexOf(value) !== -1) {
                          // Circular reference found, discard key
                        return;
                    }
                       // Store value in our collection
                        cache.push(value);
                }
                     return value;
            });
        cache = null; // Enable garbage collection
        return request;
    }
});

writeRecords函数导致&#34; TypeError:将循环结构转换为JSON。&#34; 当代码是:

writeRecords: function (request, data) {
    request.rawData = JSON.stringify(this.getUrn());

    return request;
}

更改后,如互联网所示,&#34; TypeError:对象不是函数&#34;不断出现。由于此错误,大约有23个规格和16个失败。如果你们能帮助我解决这个问题,那就太好了。

非常感谢你!很抱歉很长的帖子......

1 个答案:

答案 0 :(得分:1)

我可以告诉您,由于循环对象结构,您尝试序列化this.getUrn()函数的结果失败了。这反过来导致您的Ext对象定义失败,导致该对象不是函数错误。

修改

要解决Javascript对象的循环结构,必须检查它的特征如下:

var a = {};
a.b = a;

JSON.stringify()无法处理诸如“a”之类的对象,因为它的子节点在某些时候引用它们的父节点。 this.getUrn()的结果可能会返回一个Javascript对象,其中一个子对象引用父对象,导致出现错误。

要更正此问题,可以删除this.getUrn()函数返回的对象中的违规值或其属性。如果this.getUrn()从服务器返回值,那么根据您的使用情况,可能更希望将它们移除到服务器端。

这有帮助吗?