简体   繁体   中英

Why can't I access the property of the object?

I'm trying to access the property " providerAccountId " from the " accountsCardModel " object, but for some reason, it's undefined. I'm using Kendo Observable object, and get method usually works. I even tried using the dot notation to access the property stored within the object (no luck).

The following line, the console throws the error "Cannot read property 'get' of undefined"

provAccId: accountsCardModel.get("providerAccountId")

I don't know how Kendo internally works. Perhaps, kendo tries to execute the code before I want it to, but that's unlikely. Perhaps it's an hoisting issue.

 var accountsCardModel = kendo.observable({ providerAccountId: "", datasource: new kendo.data.DataSource({ transport: { read: { url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService", dataType: "json", type: "POST", data: { op: "read", provAccId: accountsCardModel.get("providerAccountId") }, beforeSend: function(jqXHR){ Utils.xhr.queue(jqXHR); }, complete: function(jqXHR) { Utils.xhr.dequeue(jqXHR); } } }, schema: { parse: function(response) { console.log(response); } } }), showAccountDetails: function(providerAccountId){ this.set("providerAccountId", providerAccountId); } }); 

Well, I found a solution:

Traced the call stack, and it's frustrating to see the Kendo executing my lines behind the scene without my knowledge. Instead of sending read: parameters, I called it as a function and it worked.

Before:

    datasource: new kendo.data.DataSource({
    transport: {
        read: {
            url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService",
            dataType: "json",
            type: "POST",
            data: {
                op: "read",
                provAccId: accountsCardModel.get("providerAccountId")
            },
            beforeSend: function(jqXHR){
                Utils.xhr.queue(jqXHR);
            },
            complete: function(jqXHR) {
                Utils.xhr.dequeue(jqXHR);
            }
        }
    },
    schema: {
        parse: function(response) {
            console.log(response);
        }
    }
}),

Afer:

    datasource: new kendo.data.DataSource({
    transport: {
        read: function(options) {
            $.ajax({
                url: MobileStorage.getServerURL() + "/cabinet/wicket/bookmarkable/com.web.services.AccountsService",
                dataType: "json",
                type: "POST",
                data: {
                    op: "read",
                    provAccId: accountsCardModel.providerAccountId
                },
                success: function(result) {
                  options.success(result);
                },
                error: function(result) {
                  options.error(result);
                },
                beforeSend: function(jqXHR){
                    Utils.xhr.queue(jqXHR);
                },
                complete: function(jqXHR) {
                    Utils.xhr.dequeue(jqXHR);
                }
            });
        }
    },
    schema: {
        parse: function(response) {
            console.log(response);
            //return response.providerAccounts[0]; // Load only online account, omit autologin
        }
    }
})

And it worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM